<?php

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

use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

/**
 * 鉴权自检：返回当前 Token 对应的玩家公开字段（不含密码）。
 *
 * 路由：GET /api/v1/player/me ，需 middleware lottery.player。
 *
 * 补充字段与 PRD「识别玩家」及前端引导一致：`locale` 为当次 API 实际使用的语言（与 `NegotiateLotteryLocale` 一致）；
 * 时间类为 ISO 8601 字符串，便于 H5 展示与排错。
 */
final class MeController extends Controller
{
    public function __invoke(Request $request): JsonResponse
    {
        $player = $request->lotteryPlayer();
        // 理论上不会为 null（路由已套 EnsurePlayerApi）；保留断言便于排查配置错误
        abort_if($player === null, 500, 'lottery_player missing');

        return ApiResponse::success([
            'id' => $player->id,
            'site_code' => $player->site_code,
            'site_player_id' => $player->site_player_id,
            'auth_source' => $player->auth_source,
            'funding_mode' => $player->funding_mode,
            'username' => $player->username,
            'nickname' => $player->nickname,
            'default_currency' => $player->default_currency,
            'status' => $player->status,
            'locale' => $request->lotteryLocale(),
            'last_login_at' => $player->last_login_at?->toIso8601String(),
            'created_at' => $player->created_at?->toIso8601String(),
        ]);
    }
}
