<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * 后台 API：`auth:sanctum` 之后执行，校验为 {@link AdminUser} 且未禁用；
 * 上下文可通过 `$request->lotteryAdmin()` 读取。
 */
final class EnsureAdminApi
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        if (! $user instanceof AdminUser) {
            return ApiResponse::error(
                trans('admin.unauthenticated', [], $request->lotteryLocale()),
                ErrorCode::AdminUnauthenticated->value,
                null,
                401,
            );
        }

        if ((int) $user->status !== 0) {
            return ApiResponse::error(
                trans('admin.account_disabled', [], $request->lotteryLocale()),
                ErrorCode::AdminAccountDisabled->value,
                null,
                403,
            );
        }

        $request->attributes->set('lottery_admin', $user);

        return $next($request);
    }
}
