<?php

namespace App\Domains\Auth\Controllers;

use App\Domains\Auth\Requests\GoogleTokenExchangeRequest;
use App\Domains\Auth\Resources\UserResource;
use App\Domains\Auth\Services\GoogleAuthService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class GoogleTokenExchangeController extends Controller
{
    public function __construct(private readonly GoogleAuthService $googleAuthService) {}

    public function __invoke(GoogleTokenExchangeRequest $request): JsonResponse
    {
        $authentication = $this->googleAuthService->exchange($request->validated('oauth_code'));

        return response()->json([
            'message' => 'Sesion iniciada correctamente.',
            'token' => $authentication['token'],
            'token_type' => 'Bearer',
            'user' => UserResource::make($authentication['user']),
        ]);
    }
}
