<?php

namespace App\Http\Controllers;

use App\Http\Requests\User\LoginUserRequest;
use App\Http\Requests\User\StoreUserRequest;
use App\Http\Requests\User\UpdateUserRequest;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function store(StoreUserRequest $request): JsonResponse
    {
        $userResource = new UserResource(User::create($request->validated()));

        $expirationDate = now()->addWeek();
        $token = $userResource->createToken($userResource['email'], ['*'], $expirationDate)->plainTextToken;

        return response()->json([
            'success' => true,
            'message' => 'Account created successfully.',
            'data' => [
                'user' => $userResource,
                'token' => $token,
                'token_expires_at' => $expirationDate
            ]
        ], 201);
    }

    public function login(LoginUserRequest $request): JsonResponse
    {
        $validated = $request->validated();

        if (!Auth::attempt($validated)) {
            return response()->json([
                'success' => false,
                'message' => 'Incorrect login details.'
            ], 401);
        }

        $userResource = new UserResource(Auth::user());

        $expirationDate = now()->addWeek();
        $token = $userResource->createToken(
            $userResource['email'], ['*'], $expirationDate
        )->plainTextToken;

        return response()->json([
            'success' => true,
            'message' => 'Account logged in successfully.',
            'data' => [
                'user' => $userResource,
                'token' => $token,
                'token_expires_at' => $expirationDate
            ]
        ]);
    }

    public function logout(): JsonResponse
    {
        Auth::user()->currentAccessToken()->delete();

        return response()->json([
            'success' => true,
            'message' => "Account logged out successfully."
        ]);
    }

    public function update(UpdateUserRequest $request): JsonResponse
    {
        Auth::user()->update($request->validated());

        $userResource = new UserResource(Auth::user());

        return response()->json([
            'success' => true,
            'message' => 'Account updated successfully.',
            'data' => [
                'user' => $userResource
            ]
        ]);
    }

    public function destroy(): JsonResponse {
        Auth::user()->delete();
        Auth::user()->tokens()->delete();

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

    public function getUser(): JsonResponse {
        return response()->json([
            'success' => true,
            'message' => 'Account details found successfully.',
            'data' => new UserResource(Auth::user())
        ]);
    }
}
