/**
 * REST controller for per-tenant agent skills (CRUD).
 *
 * Endpoints (JWT validated by Kong gateway):
 *   GET    /api/v1/agent/skills          → List all skills for current tenant
 *   POST   /api/v1/agent/skills          → Create a new skill
 *   PUT    /api/v1/agent/skills/:id      → Update an existing skill
 *   DELETE /api/v1/agent/skills/:id      → Delete a skill
 */
import { Controller, Get, Post, Put, Delete, Body, Param, Headers, NotFoundException } from '@nestjs/common';
import { AgentSkillService, CreateSkillDto, UpdateSkillDto } from '../../../infrastructure/services/agent-skill.service';

@Controller('api/v1/agent/skills')
export class SkillsController {
  constructor(private readonly skillService: AgentSkillService) {}

  @Get()
  async list(@Headers('x-tenant-id') tenantId: string) {
    return this.skillService.findAllByTenant(tenantId || 'default');
  }

  @Post()
  async create(
    @Headers('x-tenant-id') tenantId: string,
    @Body() dto: CreateSkillDto,
  ) {
    return this.skillService.create(tenantId || 'default', dto);
  }

  @Put(':id')
  async update(
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
    @Body() dto: UpdateSkillDto,
  ) {
    const skill = await this.skillService.update(id, tenantId || 'default', dto);
    if (!skill) {
      throw new NotFoundException('Skill not found');
    }
    return skill;
  }

  @Delete(':id')
  async remove(
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
  ) {
    const deleted = await this.skillService.delete(id, tenantId || 'default');
    if (!deleted) {
      throw new NotFoundException('Skill not found');
    }
    return { message: 'Skill deleted' };
  }
}
