<?php

namespace App\Http\Controllers\Api\V1\Admin\Dashboard;

use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Support\AdminScopeContextResolver;
use App\Services\Admin\AdminDashboardSnapshotBuilder;

/**
 * GET /api/v1/admin/dashboard — 首页仪表盘聚合数据（需登录；按权限返回子块）。
 */
final class AdminDashboardController extends Controller
{
    public function __construct(
        private readonly AdminDashboardSnapshotBuilder $dashboard,
    ) {}

    public function __invoke(Request $request): JsonResponse
    {
        $admin = $request->lotteryAdmin();
        if (! $admin instanceof AdminUser) {
            return ApiResponse::error(
                trans('admin.unauthenticated', [], $request->lotteryLocale()),
                ErrorCode::AdminUnauthenticated->value,
                null,
                401,
            );
        }

        $scope = AdminScopeContextResolver::fromRequest($request, $admin);

        return ApiResponse::success($this->dashboard->build($scope));
    }
}
