import { Controller, Post, Get, Body, Param, Query, UseGuards } from "@nestjs/common";
import { AuthGuard } from "../auth/guards/auth.guard";
import { WorkspaceGuard, PermissionGuard } from "../common/guards";
import { Workspace, RequirePermission, Permission } from "../common/decorators";
import { ConversationArchiveService } from "./conversation-archive.service";
import { IngestConversationDto, SearchConversationDto, ListConversationsDto } from "./dto";

/**
 * Controller for conversation archive endpoints.
 * All endpoints require workspace membership.
 */
@Controller("conversations")
@UseGuards(AuthGuard, WorkspaceGuard, PermissionGuard)
export class ConversationArchiveController {
  constructor(private readonly service: ConversationArchiveService) {}

  /**
   * POST /api/conversations/ingest
   * Ingest a conversation session log and auto-embed for semantic search.
   * Requires: MEMBER or higher
   */
  @Post("ingest")
  @RequirePermission(Permission.WORKSPACE_MEMBER)
  async ingest(
    @Workspace() workspaceId: string,
    @Body() dto: IngestConversationDto
  ): Promise<{ id: string }> {
    return this.service.ingest(workspaceId, dto);
  }

  /**
   * POST /api/conversations/search
   * Vector similarity search across archived conversations.
   * Requires: Any workspace member
   */
  @Post("search")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async search(
    @Workspace() workspaceId: string,
    @Body() dto: SearchConversationDto
  ): Promise<unknown> {
    return this.service.search(workspaceId, dto);
  }

  /**
   * GET /api/conversations
   * List conversation archives with filtering and pagination.
   * Requires: Any workspace member
   */
  @Get()
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findAll(
    @Workspace() workspaceId: string,
    @Query() query: ListConversationsDto
  ): Promise<unknown> {
    return this.service.findAll(workspaceId, query);
  }

  /**
   * GET /api/conversations/:id
   * Get a single conversation archive by ID (includes full messages).
   * Requires: Any workspace member
   */
  @Get(":id")
  @RequirePermission(Permission.WORKSPACE_ANY)
  async findOne(@Workspace() workspaceId: string, @Param("id") id: string): Promise<unknown> {
    return this.service.findOne(workspaceId, id);
  }
}
