<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\ReceiveLoginRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use OpenApi\Annotations as OA;
use App\Http\Responses\ApiResponse;

/**
 * @OA\Tag(
 *     name="Auth",
 *     description="用戶個人資料"
 * )
 */
class AuthController extends Controller
{
    /**
     * @OA\Post(
     *     path="/api/login",
     *     tags={"Auth"},
     *     summary="登入取得 Token",
     *     description="使用帳號密碼登入並回傳 JWT Token。",
     *     operationId="login",
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\JsonContent(ref="#/components/schemas/ReceiveLoginRequest")
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="成功傳送指令並回傳 TCP 回應",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                      @OA\Property(property="data", type="object",
     *                          @OA\Property(property="token", type="string", example="eyJhbGciOiJIUz...")
     *                      )
     *                 )
     *             }
     *         )
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                     @OA\Property(property="code", type="string", example="UNAUTHORIZED"),
     *                     @OA\Property(property="message", type="string", example="Unauthorized"),
     *                     @OA\Property(property="data", type="null")
     *                 )
     *             }
     *         )
     *     )
     * )
     */
    public function login(ReceiveLoginRequest $request)
    {
        if (!Auth::attempt($request->only('email', 'password'))) {
            return ApiResponse::unauthorized(); 
        }
        $user = Auth::user();
        // 3. 產生或取得 Token
        if (empty($user->api_plain_token)) {
            $token = $user->createToken('*')->plainTextToken;
            $user->api_plain_token = $token;
            $user->save();
        } else {
            $token = $user->api_plain_token;
        }
        return ApiResponse::success(['token' => $token,'user' => $user]);
    }

    /**
     * @OA\Get(
     *     path="/api/profile",
     *     summary="Get current user profile",
     *     tags={"Auth"},
     *     security={{"Authorization":{}}},
     *     @OA\Response(
     *         response=200,
     *         description="User profile",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                     @OA\Property(property="data", ref="#/components/schemas/User")
     *                 )
     *             }
     *         )
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                     @OA\Property(property="code", type="string", example="UNAUTHORIZED"),
     *                     @OA\Property(property="message", type="string", example="Unauthorized"),
     *                     @OA\Property(property="data", type="null")
     *                 )
     *             }
     *         )
     *     ),
     *     @OA\Parameter(
     *         name="Accept",
     *         in="header",
     *         required=true,
     *         @OA\Schema(type="string", default="application/json")
     *     )
     * )
     */
    public function profile(Request $request)
    {
        return ApiResponse::success($request->user());
    }
    /**
     * @OA\Post(
     *     path="/api/logout",
     *     summary="登出",
     *     tags={"Auth"},
     *     security={{"Authorization":{}}},
     *     @OA\Response(
     *         response=200,
     *         description="成功登出",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                     @OA\Property(property="data", type="null")
     *                 )
     *             }
     *         )
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized"
     *     )
     * )
     */
    public function logout(Request $request)
    {
        $request->user()->currentAccessToken()->delete();
        $request->user()->forceFill([
            'api_plain_token' => null,
        ])->save();
        return ApiResponse::success();
    }
}
