import {
  Controller,
  Get,
  Post,
  Body,
  Put,
  Param,
  Delete,
  Query,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { DepartmentsService } from './departments.service';
import { CreateDepartmentDto } from './dto/create-department.dto';
import { UpdateDepartmentDto } from './dto/update-department.dto';
import { QueryDepartmentsDto } from './dto/query-departments.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';

@ApiTags('departments')
@ApiBearerAuth('JWT-auth')
@Controller('departments')
export class DepartmentsController {
  constructor(private readonly departmentsService: DepartmentsService) {}

  @Post()
  @Roles('admin')
  @RequirePermissions(Permission.DEPARTMENTS_CREATE)
  @AuditLog('Department', 'CREATE')
  @ApiOperation({ summary: 'Create a new department' })
  @ApiResponse({
    status: 201,
    description: 'Department created successfully',
  })
  @ApiResponse({ status: 400, description: 'Bad request' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 409, description: 'Department code already exists' })
  create(@Body() createDto: CreateDepartmentDto) {
    return this.departmentsService.create(createDto);
  }

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

  @Get('tree')
  @RequirePermissions(Permission.DEPARTMENTS_VIEW)
  @ApiOperation({ summary: 'Get hierarchical department tree' })
  @ApiResponse({
    status: 200,
    description: 'Department tree structure',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  getTree() {
    return this.departmentsService.getTree();
  }

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

  @Get(':id/employees')
  @RequirePermissions(Permission.DEPARTMENTS_VIEW)
  @ApiOperation({ summary: 'Get employees of a department' })
  @ApiParam({ name: 'id', description: 'Department ID' })
  @ApiResponse({
    status: 200,
    description: 'List of department employees',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 404, description: 'Department not found' })
  getEmployees(
    @Param('id') id: string,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
  ) {
    return this.departmentsService.getEmployees(id, { page, limit });
  }

  @Put(':id')
  @Roles('admin')
  @RequirePermissions(Permission.DEPARTMENTS_UPDATE)
  @AuditLog('Department', 'UPDATE')
  @ApiOperation({ summary: 'Update a department' })
  @ApiParam({ name: 'id', description: 'Department ID' })
  @ApiResponse({
    status: 200,
    description: 'Department updated successfully',
  })
  @ApiResponse({ status: 400, description: 'Bad request' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Department not found' })
  @ApiResponse({ status: 409, description: 'Department code already exists' })
  update(@Param('id') id: string, @Body() updateDto: UpdateDepartmentDto) {
    return this.departmentsService.update(id, updateDto);
  }

  @Delete(':id')
  @Roles('admin')
  @RequirePermissions(Permission.DEPARTMENTS_DELETE)
  @AuditLog('Department', 'DELETE')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Deactivate a department' })
  @ApiParam({ name: 'id', description: 'Department ID' })
  @ApiResponse({
    status: 200,
    description: 'Department deactivated successfully',
  })
  @ApiResponse({ status: 400, description: 'Cannot deactivate department with active users/children' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden' })
  @ApiResponse({ status: 404, description: 'Department not found' })
  remove(@Param('id') id: string) {
    return this.departmentsService.remove(id);
  }
}
