import {
  Controller,
  Get,
  Post,
  Body,
  Query,
  Put,
  Patch,
  Delete,
  Param,
} from '@nestjs/common';
import { SkillsService } from './skills.service';
import { Public } from '../auth/public.decorator';
import { Skills } from './entities/skills.entity';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('skills')
@ApiBearerAuth('access-token')
@Controller('skills')
export class SkillsController {
  constructor(private readonly skillsService: SkillsService) {}

  // Get all skills
  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all skills' })
  async findAll(): Promise<Skills[]> {
    return this.skillsService.findAll();
  }

  @Public()
  @Get('filtered')
  @ApiOperation({ summary: 'Get Skills by Type' })
  @ApiQuery({ name: 'type', enum: ['tech', 'nontech'] })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  findByType(
    @Query('type') type: 'tech' | 'nontech',
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
  ) {
    return this.skillsService.findByType(
      type,
      Number(page || 1),
      Number(limit || 10),
    );
  }

  // Create a new skill
  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Skill' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        skill_name: { type: 'string' },
        skill_type: { type: 'string', enum: ['tech', 'nontech'] },
      },
      required: ['skill_name'],
    },
  })
  async create(@Body() body: any): Promise<Skills> {
    return this.skillsService.create(
      body.skill_name,
      body.skill_type || 'tech',
    );
  }

  // Create skill if not exists
  @Public()
  @Post('create_if_not_exists')
  @ApiOperation({ summary: 'Create Skill if Not Exists' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        skill_name: { type: 'string' },
        skill_type: { type: 'string', enum: ['tech', 'nontech'] },
      },
      required: ['skill_name'],
    },
  })
  async createIfNotExists(@Body() body: any): Promise<Skills> {
    return this.skillsService.createIfNotExists(
      body.skill_name,
      body.skill_type || 'tech',
    );
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update Skill' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        skill_name: { type: 'string' },
        skill_type: { type: 'string' },
      },
    },
  })
  update(@Param('id') id: number, @Body() body: any) {
    return this.skillsService.update(id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch Skill' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        skill_name: { type: 'string' },
        skill_type: { type: 'string' },
      },
    },
  })
  patch(@Param('id') id: number, @Body() body: any) {
    return this.skillsService.update(id, body);
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete skill' })
  @ApiParam({ name: 'id', type: Number })
  remove(@Param('id') id: number) {
    return this.skillsService.remove(id);
  }
}
