import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Inject,
  NotFoundException,
  Param,
  Patch,
  Post,
  UseGuards,
} from '@nestjs/common';
import { SkillsService } from './skills.service.js';
import { AuthGuard } from '../auth/auth.guard.js';
import type { CreateSkillDto, UpdateSkillDto } from './skills.dto.js';

@Controller('api/skills')
@UseGuards(AuthGuard)
export class SkillsController {
  constructor(@Inject(SkillsService) private readonly skills: SkillsService) {}

  @Get()
  async list() {
    return this.skills.findAll();
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const skill = await this.skills.findById(id);
    if (!skill) throw new NotFoundException('Skill not found');
    return skill;
  }

  @Post()
  async create(@Body() dto: CreateSkillDto) {
    return this.skills.create({
      name: dto.name,
      description: dto.description,
      version: dto.version,
      source: dto.source,
      config: dto.config,
      enabled: dto.enabled,
    });
  }

  @Patch(':id')
  async update(@Param('id') id: string, @Body() dto: UpdateSkillDto) {
    const skill = await this.skills.update(id, dto);
    if (!skill) throw new NotFoundException('Skill not found');
    return skill;
  }

  @Patch(':id/toggle')
  async toggle(@Param('id') id: string, @Body() body: { enabled: boolean }) {
    const skill = await this.skills.toggle(id, body.enabled);
    if (!skill) throw new NotFoundException('Skill not found');
    return skill;
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: string) {
    const deleted = await this.skills.remove(id);
    if (!deleted) throw new NotFoundException('Skill not found');
  }
}
