<?php

namespace App\Http\Controllers\api\v1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ChangePasswordRequest;
use App\Http\Requests\Admin\UpdateProfileRequest;
use App\Service\Admin\AdminService;
use App\Service\Admin\AdminProfileService;
use App\Traits\JsonResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    use JsonResponseTrait;

    public function __construct(
        protected AdminService $adminService,
        protected AdminProfileService $adminProfileService
    ) {}

    public function getProfile(Request $request)
    {
        $auth_user = $request->auth_user;
        return $this->adminProfileService->getAdminProfile($auth_user);
    }

    /**
     * Change the authenticated user's password.
     *
     * @param  ChangePasswordRequest  $request
     * @return JsonResponse
     */
    public function changePassword(ChangePasswordRequest $request)
    {
        $authUser = $request->auth_user;
        $input = $request->validated();
        $result = $this->adminService->changePassword($authUser, $input['current_password'], $input['new_password']);

        return $result;
    }

    /**
     * Update the authenticated user's profile.
     *
     * @param  UpdateProfileRequest  $request
     * @return JsonResponse
     */
    public function updateProfile(UpdateProfileRequest $request)
    {
        $authUser = $request->auth_user;
        $input = $request->validated();
        return $this->adminProfileService->updateProfileAfterFirstLogin($authUser, $input);
    }

    /**
     * Check if admin needs to update profile
     */
    public function checkProfileUpdate(Request $request)
    {
        $auth_user = $request->auth_user;
        $needsUpdate = $this->adminProfileService->needsProfileUpdate($auth_user);

        return $this->successDataResponse(
            data: [
                'needs_update' => $needsUpdate,
                'user' => $auth_user,
            ],
            message: "Profile update status checked"
        );
    }
}
