import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  Delete,
  Query,
  HttpCode,
  HttpStatus,
  Req,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { SkillsService } from './skills.service';
import { CreateSkillDto, UpdateSkillDto, QuerySkillsDto } from './dto';
import { Roles } from '../../../auth/decorators/roles.decorator';
import { RequirePermissions } from '../../../auth/permissions/permissions.decorator';
import { Permission } from '../../../auth/permissions/permissions.enum';
import { AuditLog } from '../../audit/audit.interceptor';
import { Request } from 'express';

interface AuthenticatedRequest extends Request {
  user: {
    id: string;
    email: string;
    [key: string]: any;
  };
}

@ApiTags('lean/skills')
@ApiBearerAuth('JWT-auth')
@Controller('lean/skills')
export class SkillsController {
  constructor(private readonly skillsService: SkillsService) {}

  @Post()
  @Roles('admin', 'hr-manager')
  @RequirePermissions(Permission.SKILLS_MANAGE)
  @AuditLog('Skill', 'CREATE')
  @ApiOperation({ summary: 'Create a new skill' })
  @ApiResponse({
    status: 201,
    description: 'Skill created successfully',
  })
  @ApiResponse({ status: 400, description: 'Bad request' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 409, description: 'Skill already exists' })
  create(@Body() createDto: CreateSkillDto, @Req() req: AuthenticatedRequest) {
    return this.skillsService.create(createDto, req.user.id);
  }

  @Get()
  @RequirePermissions(Permission.SKILLS_VIEW)
  @ApiOperation({ summary: 'Get all skills with pagination and filtering' })
  @ApiResponse({
    status: 200,
    description: 'List of skills',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  findAll(@Query() query: QuerySkillsDto) {
    return this.skillsService.findAll(query);
  }

  @Get('categories')
  @RequirePermissions(Permission.SKILLS_VIEW)
  @ApiOperation({ summary: 'Get all skill categories' })
  @ApiResponse({
    status: 200,
    description: 'List of categories',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  getCategories() {
    return this.skillsService.getCategories();
  }

  @Get('department/:departmentId')
  @RequirePermissions(Permission.SKILLS_VIEW)
  @ApiOperation({ summary: 'Get skills for a specific department' })
  @ApiParam({ name: 'departmentId', description: 'Department ID' })
  @ApiResponse({
    status: 200,
    description: 'List of skills for the department',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Department not found' })
  getSkillsForDepartment(@Param('departmentId') departmentId: string) {
    return this.skillsService.getSkillsForDepartment(departmentId);
  }

  @Get(':id')
  @RequirePermissions(Permission.SKILLS_VIEW)
  @ApiOperation({ summary: 'Get a skill by ID' })
  @ApiParam({ name: 'id', description: 'Skill ID' })
  @ApiResponse({
    status: 200,
    description: 'Skill found',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Skill not found' })
  findOne(@Param('id') id: string) {
    return this.skillsService.findOne(id);
  }

  @Put(':id')
  @Roles('admin', 'hr-manager')
  @RequirePermissions(Permission.SKILLS_MANAGE)
  @AuditLog('Skill', 'UPDATE')
  @ApiOperation({ summary: 'Update a skill' })
  @ApiParam({ name: 'id', description: 'Skill ID' })
  @ApiResponse({
    status: 200,
    description: 'Skill updated successfully',
  })
  @ApiResponse({ status: 400, description: 'Bad request' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Skill not found' })
  @ApiResponse({ status: 409, description: 'Skill name already exists' })
  update(@Param('id') id: string, @Body() updateDto: UpdateSkillDto) {
    return this.skillsService.update(id, updateDto);
  }

  @Delete(':id')
  @Roles('admin', 'hr-manager')
  @RequirePermissions(Permission.SKILLS_MANAGE)
  @AuditLog('Skill', 'DELETE')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Deactivate a skill' })
  @ApiParam({ name: 'id', description: 'Skill ID' })
  @ApiResponse({
    status: 200,
    description: 'Skill deactivated successfully',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Skill not found' })
  remove(@Param('id') id: string) {
    return this.skillsService.remove(id);
  }
}
