<?php

namespace App\Http\Controllers\Api\V1\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Auth\LoginRequest;
use App\Http\Requests\Api\V1\Auth\RegisterRequest;
use App\Http\Resources\Api\V1\UserResource;
use App\Services\AuthService;
use App\Services\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    protected AuthService $authService;
    protected ResponseService $responseService;

    public function __construct(AuthService $authService, ResponseService $responseService)
    {
        $this->authService = $authService;
        $this->responseService = $responseService;
    }

    public function register(RegisterRequest $request): JsonResponse
    {
        try {
            $result = $this->authService->register($request->validated());

            return $this->responseService->created([
                'user' => new UserResource($result['user']),
                'token' => $result['token'],
            ], 'User registered successfully');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Registration failed',
                $e->getMessage()
            );
        }
    }

    public function login(LoginRequest $request): JsonResponse
    {
        try {
            $result = $this->authService->login($request->validated());

            return $this->responseService->success([
                'user' => new UserResource($result['user']),
                'token' => $result['token'],
            ], 'Login successful');
        } catch (\Illuminate\Validation\ValidationException $e) {
            return $this->responseService->validationError(
                $e->errors(),
                'Invalid credentials'
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Login failed',
                $e->getMessage()
            );
        }
    }

    public function logout(Request $request): JsonResponse
    {
        try {
            $this->authService->logout($request->user());

            return $this->responseService->success(null, 'Logged out successfully');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Logout failed',
                $e->getMessage()
            );
        }
    }

    public function user(Request $request): JsonResponse
    {
        try {
            $user = $this->authService->getAuthenticatedUser($request->user());

            return $this->responseService->success(
                new UserResource($user),
                'User retrieved successfully'
            );
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Failed to retrieve user',
                $e->getMessage()
            );
        }
    }

    public function refreshToken(Request $request): JsonResponse
    {
        try {
            $token = $this->authService->refreshToken($request->user());

            return $this->responseService->success([
                'token' => $token,
            ], 'Token refreshed successfully');
        } catch (\Exception $e) {
            return $this->responseService->serverError(
                'Token refresh failed',
                $e->getMessage()
            );
        }
    }
}
