import { Controller, Get, Post, Body, Param, UseGuards, Request } from "@nestjs/common";
import { SkipThrottle as SkipThrottler } from "@nestjs/throttler";
import { WidgetsService } from "./widgets.service";
import { WidgetDataService } from "./widget-data.service";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard } from "../common/guards/workspace.guard";
import { StatCardQueryDto, ChartQueryDto, ListQueryDto, CalendarPreviewQueryDto } from "./dto";
import type { RequestWithWorkspace } from "../common/types/user.types";

/**
 * Controller for widget definition and data endpoints
 * All endpoints require authentication; data endpoints also require workspace context
 */
@Controller("widgets")
@UseGuards(AuthGuard)
export class WidgetsController {
  constructor(
    private readonly widgetsService: WidgetsService,
    private readonly widgetDataService: WidgetDataService
  ) {}

  /**
   * GET /api/widgets
   * List all available widget definitions
   * Returns only active widgets
   */
  @Get()
  async findAll() {
    return this.widgetsService.findAll();
  }

  /**
   * GET /api/widgets/:name
   * Get a widget definition by name
   * Useful for fetching widget configuration schemas
   */
  @Get(":name")
  async findByName(@Param("name") name: string) {
    return this.widgetsService.findByName(name);
  }

  /**
   * POST /api/widgets/data/stat-card
   * Get stat card widget data
   */
  @Post("data/stat-card")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getStatCardData(@Request() req: RequestWithWorkspace, @Body() query: StatCardQueryDto) {
    return this.widgetDataService.getStatCardData(req.workspace.id, query);
  }

  /**
   * POST /api/widgets/data/chart
   * Get chart widget data
   */
  @Post("data/chart")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getChartData(@Request() req: RequestWithWorkspace, @Body() query: ChartQueryDto) {
    return this.widgetDataService.getChartData(req.workspace.id, query);
  }

  /**
   * POST /api/widgets/data/list
   * Get list widget data
   */
  @Post("data/list")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getListData(@Request() req: RequestWithWorkspace, @Body() query: ListQueryDto) {
    return this.widgetDataService.getListData(req.workspace.id, query);
  }

  /**
   * POST /api/widgets/data/calendar-preview
   * Get calendar preview widget data
   */
  @Post("data/calendar-preview")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getCalendarPreviewData(
    @Request() req: RequestWithWorkspace,
    @Body() query: CalendarPreviewQueryDto
  ) {
    return this.widgetDataService.getCalendarPreviewData(req.workspace.id, query);
  }

  /**
   * POST /api/widgets/data/active-projects
   * Get active projects widget data
   */
  @Post("data/active-projects")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getActiveProjectsData(@Request() req: RequestWithWorkspace) {
    return this.widgetDataService.getActiveProjectsData(req.workspace.id);
  }

  /**
   * POST /api/widgets/data/agent-chains
   * Get agent chains widget data (active agent sessions)
   */
  @Post("data/agent-chains")
  @SkipThrottler()
  @UseGuards(WorkspaceGuard)
  async getAgentChainsData(@Request() req: RequestWithWorkspace) {
    return this.widgetDataService.getAgentChainsData(req.workspace.id);
  }
}
