import { Controller, Get, Post, Body, Query, UseGuards } from "@nestjs/common";
import { BrainService } from "./brain.service";
import { IntentClassificationService } from "./intent-classification.service";
import {
  BrainQueryDto,
  BrainSearchDto,
  BrainContextDto,
  ClassifyIntentDto,
  IntentClassificationResultDto,
} from "./dto";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, Permission, RequirePermission } from "../common/decorators";

/**
 * @description Controller for AI/brain operations on workspace data.
 * Provides endpoints for querying, searching, and getting context across
 * tasks, events, and projects within a workspace.
 */
@Controller("brain")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class BrainController {
  constructor(
    private readonly brainService: BrainService,
    private readonly intentClassificationService: IntentClassificationService
  ) {}

  /**
   * @description Query workspace entities with flexible filtering options.
   * Allows filtering tasks, events, and projects by various criteria.
   * @param queryDto - Query parameters including entity types, filters, and search term
   * @param workspaceId - The workspace ID (injected from request context)
   * @returns Filtered tasks, events, and projects with metadata
   * @throws UnauthorizedException if user lacks workspace access
   * @throws ForbiddenException if user lacks required permissions
   */
  @Post("query")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async query(@Body() queryDto: BrainQueryDto, @Workspace() workspaceId: string) {
    return this.brainService.query(Object.assign({}, queryDto, { workspaceId }));
  }

  /**
   * @description Get current workspace context for AI operations.
   * Returns a summary of active tasks, overdue items, upcoming events, and projects.
   * @param contextDto - Context options specifying which entities to include
   * @param workspaceId - The workspace ID (injected from request context)
   * @returns Workspace context with summary counts and optional detailed entity lists
   * @throws UnauthorizedException if user lacks workspace access
   * @throws ForbiddenException if user lacks required permissions
   * @throws NotFoundException if workspace does not exist
   */
  @Get("context")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async getContext(@Query() contextDto: BrainContextDto, @Workspace() workspaceId: string) {
    return this.brainService.getContext(Object.assign({}, contextDto, { workspaceId }));
  }

  /**
   * @description Search across all workspace entities by text.
   * Performs case-insensitive search on titles, descriptions, and locations.
   * @param searchTerm - Text to search for across all entity types
   * @param limit - Maximum number of results per entity type (max: 100, default: 20)
   * @param workspaceId - The workspace ID (injected from request context)
   * @returns Matching tasks, events, and projects with metadata
   * @throws UnauthorizedException if user lacks workspace access
   * @throws ForbiddenException if user lacks required permissions
   */
  @Get("search")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async search(@Query() searchDto: BrainSearchDto, @Workspace() workspaceId: string) {
    const searchTerm = searchDto.q ?? "";
    const limit = searchDto.limit ?? 20;
    return this.brainService.search(workspaceId, searchTerm, limit);
  }

  /**
   * @description Classify a natural language query into a structured intent.
   * Uses hybrid classification: rule-based (fast) with optional LLM fallback.
   * @param dto - Classification request with query and optional useLlm flag
   * @returns Intent classification with confidence, entities, and method used
   * @throws UnauthorizedException if user lacks workspace access
   * @throws ForbiddenException if user lacks required permissions
   */
  @Post("classify")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async classifyIntent(@Body() dto: ClassifyIntentDto): Promise<IntentClassificationResultDto> {
    return this.intentClassificationService.classify(dto.query, dto.useLlm);
  }
}
