import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Request,
} from '@nestjs/common';
import type { Request as ExpressRequest } from 'express';
import { AgentPersonasService } from '../services/personas.service';
import { resolveActor } from '../utils/auth-resolution.util';

@Controller('agent/personas')
export class AgentPersonasController {
  constructor(private readonly personasService: AgentPersonasService) {}

  @Get()
  async list(@Request() req: ExpressRequest) {
    const { orgId, userId } = resolveActor(req);
    const items = await this.personasService.list(orgId, userId);
    return { items };
  }

  @Post()
  async create(
    @Body()
    body: {
      name: string;
      icon?: string;
      description?: string;
      instructions?: string;
      allowedTools?: string[];
    },
    @Request() req: ExpressRequest,
  ) {
    const { orgId, userId } = resolveActor(req);
    return this.personasService.create({
      organizationId: orgId,
      createdById: userId,
      ...body,
    });
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body()
    body: {
      name?: string;
      icon?: string;
      description?: string;
      instructions?: string;
      allowedTools?: string[];
      enabled?: boolean;
    },
    @Request() req: ExpressRequest,
  ) {
    const { orgId, userId } = resolveActor(req);
    return this.personasService.update(id, orgId, userId, body);
  }

  @Delete(':id')
  async remove(@Param('id') id: string, @Request() req: ExpressRequest) {
    const { orgId, userId } = resolveActor(req);
    return this.personasService.remove(id, orgId, userId);
  }
}
