import { Controller, Get, UseGuards, BadRequestException } from "@nestjs/common";
import { DashboardService } from "./dashboard.service";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";
import { DashboardSummaryDto } from "./dto";

/**
 * Controller for dashboard endpoints.
 * Returns aggregated summary data for the workspace dashboard.
 *
 * Guards are applied in order:
 * 1. AuthGuard - Verifies user authentication
 * 2. WorkspaceGuard - Validates workspace access and sets RLS context
 * 3. PermissionGuard - Checks role-based permissions
 */
@Controller("dashboard")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class DashboardController {
  constructor(private readonly dashboardService: DashboardService) {}

  /**
   * GET /api/dashboard/summary
   * Returns aggregated metrics, recent activity, active jobs, and token budgets
   * Requires: Any workspace member (including GUEST)
   */
  @Get("summary")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async getSummary(@Workspace() workspaceId: string | undefined): Promise<DashboardSummaryDto> {
    if (!workspaceId) {
      throw new BadRequestException("Workspace context required");
    }
    return this.dashboardService.getSummary(workspaceId);
  }
}
