<?php

namespace Modules\Auth\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Auth\DTO\UserListFilter;
use Modules\Auth\Http\Requests\User\StoreUserRequest;
use Modules\Auth\Http\Requests\User\UpdateUserRequest;
use Modules\Auth\Http\Requests\User\UpdateUserRolesRequest;
use Modules\Auth\Services\UserService;
use Modules\Auth\Transformers\UserResource;
use Modules\Core\Helpers\ApiResponse;

class UserController extends Controller
{
    use AuthorizesRequests;

    protected $userService;

    public function __construct(UserService $userService) {
        $this->userService = $userService;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('viewAnyss', User::class);

        $filters = UserListFilter::fromRequest($request);

        $response = $this->userService->list($filters);

        return (UserResource::collection($response))->additional([
            'status' => true,
            'message' => 'Users retrieved successfully.',
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreUserRequest $request)
    {
        $this->authorize('create', User::class);

        $response = $this->userService->store($request->validated());

        return ApiResponse::successResponse(
            message: 'User created successfully.',
            data: new UserResource($response)
        );
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        $user = $this->userService->find($id);

        $this->authorize('view', $user);

        return (new UserResource($user->load(
            'roles:id,name,title',
            'employee:id,user_id,emp_number,name'
        )))->additional([
            'status' => true,
            'message' => 'User retrieved successfully.',
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateUserRequest $request, $id)
    {
        $user = $this->userService->find($id);

        $this->authorize('update', $user);

        $response = $this->userService->update(
            $user,
            $request->validated()
        );

        return ApiResponse::successResponse(
            message: 'User updated successfully.',
            data: new UserResource($response)
        );
    }

    public function updateRoles(UpdateUserRolesRequest $request, $id)
    {
        $user = $this->userService->find($id);

        $this->authorize('updateRoles', $user);

        $response = $this->userService->updateRoles(
            $user,
            $request->roles
        );

        return ApiResponse::successResponse(
            message: 'User roles updated successfully.',
            data: new UserResource($response)
        );
    }

    public function toggleStatus($id)
    {
        $user = $this->userService->find($id);

        $this->authorize('updateStatus', $user);
        $this->userService->toggleStatus($user);

        return ApiResponse::successResponse(
            message: 'User status updated successfully.'
        );
    }

    public function resetPassword($id)
    {
        $user = $this->userService->find($id);

        $this->authorize('update', $user);

        $this->userService->passwordReset($user);

        return ApiResponse::successResponse(
            message: 'Password has been reset to the default password.',
            data: [
                'default_password' => config('auth.default_password'),
            ]
        );
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id) {}
}
