import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  UseGuards,
  Req,
  HttpCode,
  HttpStatus,
} from "@nestjs/common";
import { AuthGuard } from "../auth/guards/auth.guard";
import { PersonalitiesService } from "./personalities.service";
import { CreatePersonalityDto, UpdatePersonalityDto } from "./dto";
import { Personality } from "./entities/personality.entity";

interface AuthenticatedRequest {
  user: { id: string };
  workspaceId: string;
}

/**
 * Controller for managing personality/assistant configurations
 */
@Controller("personality")
@UseGuards(AuthGuard)
export class PersonalitiesController {
  constructor(private readonly personalitiesService: PersonalitiesService) {}

  /**
   * List all personalities for the workspace
   */
  @Get()
  async findAll(@Req() req: AuthenticatedRequest): Promise<Personality[]> {
    return this.personalitiesService.findAll(req.workspaceId);
  }

  /**
   * Get the default personality for the workspace
   */
  @Get("default")
  async findDefault(@Req() req: AuthenticatedRequest): Promise<Personality> {
    return this.personalitiesService.findDefault(req.workspaceId);
  }

  /**
   * Get a personality by its unique name
   */
  @Get("by-name/:name")
  async findByName(
    @Req() req: AuthenticatedRequest,
    @Param("name") name: string
  ): Promise<Personality> {
    return this.personalitiesService.findByName(req.workspaceId, name);
  }

  /**
   * Get a personality by ID
   */
  @Get(":id")
  async findOne(@Req() req: AuthenticatedRequest, @Param("id") id: string): Promise<Personality> {
    return this.personalitiesService.findOne(req.workspaceId, id);
  }

  /**
   * Create a new personality
   */
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async create(
    @Req() req: AuthenticatedRequest,
    @Body() dto: CreatePersonalityDto
  ): Promise<Personality> {
    return this.personalitiesService.create(req.workspaceId, dto);
  }

  /**
   * Update a personality
   */
  @Patch(":id")
  async update(
    @Req() req: AuthenticatedRequest,
    @Param("id") id: string,
    @Body() dto: UpdatePersonalityDto
  ): Promise<Personality> {
    return this.personalitiesService.update(req.workspaceId, id, dto);
  }

  /**
   * Delete a personality
   */
  @Delete(":id")
  @HttpCode(HttpStatus.NO_CONTENT)
  async delete(@Req() req: AuthenticatedRequest, @Param("id") id: string): Promise<void> {
    return this.personalitiesService.delete(req.workspaceId, id);
  }

  /**
   * Set a personality as the default
   */
  @Post(":id/set-default")
  async setDefault(
    @Req() req: AuthenticatedRequest,
    @Param("id") id: string
  ): Promise<Personality> {
    return this.personalitiesService.setDefault(req.workspaceId, id);
  }
}
