<?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\Http\Requests\Admin\DashboardAnalyticsRequest;
use App\Services\Admin\AdminDashboardAnalyticsBuilder;

/**
 * GET /api/v1/admin/dashboard/analytics — 可筛选区间的财务趋势与玩法拆解。
 */
final class AdminDashboardAnalyticsController extends Controller
{
    public function __construct(
        private readonly AdminDashboardAnalyticsBuilder $analytics,
    ) {}

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

        $payload = $this->analytics->build($admin, $request->filters());
        if ($payload === null) {
            return ApiResponse::error(
                trans('admin.forbidden', [], $request->lotteryLocale()),
                ErrorCode::AdminForbidden->value,
                null,
                403,
            );
        }

        return ApiResponse::success($payload);
    }
}
