<?php

namespace Wave\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserNotificationSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Tymon\JWTAuth\Facades\JWTAuth;

class SettingsController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function changePassword(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'password' => 'required',
            'new_password' => 'required|confirmed',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

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

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

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

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

    public function getNotificationSettings()
    {
        $current_user = JWTAuth::user();
        return response()->json([
            'message' => 'Terms and conditions has successfully retrieved!',
            'notification_settings' => [
                'user_push_notification' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_PUSH_NOTIFICATION)->where('is_active', true)->exists(),
                'user_bookkeeping_queue' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_BOOKKEEPING_QUEUE)->where('is_active', true)->exists(),
                'user_company_security_queue' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_COMPANY_SECURTY_QUEUE)->where('is_active', true)->exists(),
                'user_chat_room' => UserNotificationSetting::where('user_id', $current_user->id)->where('notification', UserNotificationSetting::USER_CHAT_ROOM)->where('is_active', true)->exists(),
            ],
        ]);
    }

    public function toggleNotificationStatus(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'notification' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

        $current_user = JWTAuth::user();
        $set_status = true;

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

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

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