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

@ApiTags('roles')
@ApiBearerAuth('access-token')
@Controller('roles')
export class RolesController {
  constructor(private readonly rolesService: RolesService) {}

  @Public()
  @Get('with_open_positions')
  @ApiOperation({ summary: 'Get roles with open positions' })
  async getRolesWithOpenPositions() {
    return this.rolesService.getRolesWithOpenPositions();
  }

  @Public()
  @Get('popular')
  @ApiOperation({ summary: 'Get popular roles' })
  async getPopularRoles() {
    return this.rolesService.getPopularRoles();
  }
  @Public()
  @Get('mock')
  @ApiOperation({ summary: 'Get mock roles' })
  async getMockRoles() {
    return this.rolesService.getMockRoles();
  }
  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all roles' })
  findAll() {
    return this.rolesService.findAll();
  }

  //   @Public()
  //   @Get('filtered')
  //   @ApiOperation({ summary: 'Get Roles Paginated' })
  //   @ApiQuery({ name: 'page', required: false, type: Number })
  //   @ApiQuery({ name: 'limit', required: false, type: Number })
  //   @ApiQuery({ name: 'role_level_id', required: false, type: Number })
  //   @ApiQuery({ name: 'search', required: false, type: String })
  //   findAllPaginated(
  //     @Query('page') page: number = 1,
  //     @Query('limit') limit: number = 10,
  //     @Query('role_level_id') roleLevelId?: number,
  //     @Query('search') search?: string,
  //   ) {
  //     return this.rolesService.findAllPaginated(
  //       Number(page || 1),
  //       Number(limit || 10),
  //       roleLevelId ? Number(roleLevelId) : undefined,
  //       search,
  //     );
  //   }

  // @Public()
  //   @Get('filtered')
  //   @ApiOperation({ summary: 'Get Roles Paginated' })
  //   @ApiQuery({ name: 'page', required: false, type: Number })
  //   @ApiQuery({ name: 'limit', required: false, type: Number })
  //   @ApiQuery({ name: 'role_level_id', required: false, type: Number })
  //   @ApiQuery({ name: 'search', required: false, type: String })
  //   @ApiQuery({ name: 'category', required: false, enum: ['blue_collar', 'white_collar'], description: 'Filter roles by collar type' })
  //   findAllPaginated(
  //     @Query('page') page: number = 1,
  //     @Query('limit') limit: number = 10,
  //     @Query('role_level_id') roleLevelId?: number,
  //     @Query('search') search?: string,
  //     @Query('category') category?: 'blue_collar' | 'white_collar',
  //   ) {
  //     return this.rolesService.findAllPaginated(
  //       Number(page || 1),
  //       Number(limit || 10),
  //       roleLevelId ? Number(roleLevelId) : undefined,
  //       search,
  //       category,
  //     );
  //   }

  @Public()
  @Get('filtered')
  @ApiOperation({ summary: 'Get Roles Paginated' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'role_level_id', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({
    name: 'category',
    required: false,
    enum: ['blue_collar', 'white_collar'],
    description: 'Filter roles by collar type',
  })
  findAllPaginated(
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 20,
    @Query('role_level_id') roleLevelId?: number,
    @Query('search') search?: string,
    @Query('category') category?: 'blue_collar' | 'white_collar',
  ) {
    return this.rolesService.findAllPaginated(
      Number(page || 1),
      Number(limit || 10),
      roleLevelId ? Number(roleLevelId) : undefined,
      search,
      category,
    );
  }

  @Public()
  @Get(':id')
  @ApiOperation({ summary: 'Get role by ID' })
  @ApiParam({ name: 'id', type: String })
  findOne(@Param('id') id: string) {
    return this.rolesService.findOne(+id);
  }

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Role' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        category: { type: 'string', enum: ['blue_collar', 'white_collar'] },
      },
      required: ['name'],
    },
  })
  create(@Body() body: any) {
    return this.rolesService.create(body);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update Role' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        category: { type: 'string', enum: ['blue_collar', 'white_collar'] },
      },
    },
  })
  update(@Param('id') id: string, @Body() body: any) {
    return this.rolesService.update(+id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch Role' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        category: { type: 'string', enum: ['blue_collar', 'white_collar'] },
      },
    },
  })
  patch(@Param('id') id: string, @Body() body: any) {
    return this.rolesService.update(+id, body);
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete role' })
  @ApiParam({ name: 'id', type: String })
  remove(@Param('id') id: string) {
    return this.rolesService.remove(+id);
  }
}
