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

@Controller('agent/projects')
export class AgentProjectsController {
  constructor(private readonly projectsService: AgentProjectsService) {}

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

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

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() body: { name?: string; icon?: string; color?: string; instructions?: string },
    @Request() req: ExpressRequest,
  ) {
    const { orgId, userId } = resolveActor(req);
    return this.projectsService.update(id, orgId, userId, body);
  }

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