<?php

namespace Wave\Http\Controllers\Settings;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use App\Models\User;
use App\Models\UserNotificationSetting;
use App\Models\OnlineHelp;
use App\Models\UserEnquiry;
use Illuminate\Http\Request;

class SettingsController extends Controller
{
    public function main()
    {
        $current_user = \Auth::user();
        $siteSetting = SiteSetting::latest()->first();
        $faq_lists = OnlineHelp::orderBy('sort', 'asc')->get();

        return view('theme::settings.main', [
            'siteSetting' => $siteSetting,
            'user_push_notification' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_PUSH_NOTIFICATION)->first(),
            'user_bookkeeping_queue' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_BOOKKEEPING_QUEUE)->first(),
            'user_company_security_queue' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_COMPANY_SECURTY_QUEUE)->first(),
            'user_chat_room' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_CHAT_ROOM)->first(),
            'UserNotificationSetting' => UserNotificationSetting::class,
            'faq_lists' => $faq_lists,
        ]);
    }

    public function myEnquiries()
    {
        $current_user = \Auth::user();
        $enquiry_list = UserEnquiry::where('user_id', $current_user->id)->get();

        return view('theme::settings.my-enquiries', [
            'enquiry_list' => $enquiry_list,
        ]);
    }

    public function enquiryDetails()
    {
        return view('theme::settings.enquiry-details');
    }

    public function toggleNotificationStatus($notification)
    {
        $current_user = \Auth::user();
        $set_status = true;

        $check_settings = UserNotificationSetting::where('user_id', $current_user->id)->where('notification', $notification)->first();
        if ($check_settings) {
            $set_status = !$check_settings->is_active;
        }

        UserNotificationSetting::updateOrCreate(
            [
                'user_id' => $current_user->id,
                'notification' => $notification,
            ],
            [
                'is_active' => $set_status,
            ],
        );

        return response()->json([
            'success' => true,
            'message' => 'notification status successfully updated',
        ]);
    }

    public function changePassword(Request $request)
    {
        $request->validate([
            'password' => 'required',
            'new_password' => 'required',
            'confirm_password' => 'required',
        ]);

        $current_user = \Auth::user();
        $new_password = \Hash::make($request->get('new_password'));

        if (! \Hash::check($request->get('password'), $current_user->password)) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid current password.',
            ]);
        }

        if (! \Hash::check($request->get('confirm_password'), $new_password)) {
            return response()->json([
                'success' => false,
                'message' => 'Password did not match.',
            ]);
        }

        User::where('id', $current_user->id)->update([
            'password' => $new_password,
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Password successfully changed.',
        ]);
    }

    public function addEnquiry(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'phone' => 'required',
            'email' => 'required',
            'category' => 'required',
            'title' => 'required',
            'message' => 'required',
        ]);

        $current_user = \Auth::user();
        $data = $request->all();
        $data['user_id'] = $current_user->id;

        UserEnquiry::create($data);

        return response()->json([
            'success' => true,
            'message' => 'Enquiry successfully created.',
        ]);

    }
}
