<?php

namespace Modules\Admin\app\Http\Controllers;

use App\Helper\Cache\CustomThemeCacheHelper;
use App\Helper\Cache\SettingCacheHelper;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\Admin\app\Emails\ForgotPasswordMail;
use Modules\Admin\app\Emails\ResetPasswordMail;
use Modules\Admin\app\Models\Admin;
use Modules\Admin\app\Models\Banner;
use Modules\Admin\app\Models\CustomTheme;
use Modules\Admin\app\Models\Setting;
use Modules\Admin\app\Rules\PasswordRule;
use Tymon\JWTAuth\Facades\JWTAuth;

class AdminController extends Controller
{

    public function __construct()
    {
        Config::set('auth.guards.api.provider', 'admin');
    }

    protected function respondWithToken()
    {
        $token = Admin::getTokenByAuth();
        return response()->json([
            'expires_in' => JWTAuth::setToken($token)->getPayload()->get('exp'),
            'token' => $token,
            'user' => auth('admins')->user(),
            'status' => true
        ]);
    }

    public function login(Request $request)
    {
        $email = $request->get('email');
        $password = $request->get('password');
        $admin = Admin::where('email', $email)->first();
        if ($admin && Hash::check($password, $admin->password)) {
            auth('admins')->login($admin);
            return $this->respondWithToken();
        }

        return response()->json([
            'message' => 'Incorrect email or password.',
            'status' => false
        ], 400);
    }

    public function register()
    {
    }

    public function forgotPassword(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email'
        ]);

        $admin = Admin::where('email', $request->get('email'))->first();
        if ($admin) {
            $admin->update([
                'forgot_code' => Str::random()
            ]);

            Mail::send(new ForgotPasswordMail(
                name: $admin->name,
                email: $admin->email,
                forgot_code: $admin->forgot_code
            ));
        }

        return response()->json([
            'status' => true
        ]);
    }

    public function resetPassword(Request $request)
    {
        $this->validate($request, [
            'forgot_code' => 'required|exists:admin',
            'new_password' =>  new PasswordRule,
            'confirm_password' => 'same:new_password'
        ]);

        $admin = Admin::where('forgot_code', $request->get('forgot_code'))->first();
        if ($admin) {
            $admin->update([
                'password' => bcrypt($request->get('new_password')),
                'forgot_code' => null
            ]);

            Mail::send(new ResetPasswordMail(
                name: $admin->name,
                email: $admin->email,
            ));

            return response()->json([
                'status' => true
            ]);
        }


        return response()->json([
            'status' => false
        ]);
    }

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

        $admin = auth('admins')->user();
        if (!Hash::check($request->get('password'), $admin->password)) {
            // Fail check old password
            return response()->json([
                'errors' => [
                    'password' => [
                        'The old password is incorrect'
                    ]
                ],
            ], 400);
        }

        $admin->update([
            'password' => bcrypt($request->get('new_password')),
            'forgot_code' => null
        ]);

        Mail::send(new ResetPasswordMail(
            name: $admin->name,
            email: $admin->email,
        ));

        return response()->json([
            'status' => true
        ]);
    }

    public function detail()
    {
        return response()->json([
            'user' => auth('admins')->user(),
            'status' => true
        ]);
    }

    public function logout()
    {
        $user = auth('admins')->user();
        auth('admins')->logout();

        return response()->json([
            'status' => true,
            'user' => $user
        ]);
    }
}
