import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiTags,
} from '@nestjs/swagger';
import { AppPermission } from '@aechr/shared';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Permissions as PermissionsDecorator } from '../../common/decorators/permissions.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import type { JwtAuthUser } from '../../common/interfaces/jwt-auth-user.interface';
import { ApiJwtErrorResponses } from '../../common/swagger/api-error-responses.decorator';
import {
  ApiEnvelopeCreatedResponse,
  ApiEnvelopeOkResponse,
  ApiPaginatedEnvelopeOkResponse,
} from '../../common/swagger/api-success-responses.decorator';
import { DepartmentResponseDto } from '../../common/swagger/response-models.dto';
import { CreateDepartmentDto } from './dto/create-department.dto';
import { DepartmentQueryDto } from './dto/department-query.dto';
import { UpdateDepartmentDto } from './dto/update-department.dto';
import { DepartmentsService } from './departments.service';

@ApiTags('Departments')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('departments')
export class DepartmentsController {
  constructor(private readonly departmentsService: DepartmentsService) {}

  @Get()
  @PermissionsDecorator(AppPermission.DEPARTMENTS_READ)
  @ApiOperation({ summary: 'List departments' })
  @ApiPaginatedEnvelopeOkResponse(DepartmentResponseDto, 'Paginated department list')
  @ApiJwtErrorResponses()
  list(@Query() query: DepartmentQueryDto) {
    return this.departmentsService.list(query);
  }

  @Post()
  @PermissionsDecorator(AppPermission.DEPARTMENTS_WRITE)
  @ApiOperation({ summary: 'Create department' })
  @ApiEnvelopeCreatedResponse(DepartmentResponseDto, 'Department created successfully')
  @ApiJwtErrorResponses({ conflict: 'Department name or code already exists' })
  create(@Body() body: CreateDepartmentDto, @CurrentUser() user: JwtAuthUser) {
    return this.departmentsService.create(body, user);
  }

  @Get(':id')
  @PermissionsDecorator(AppPermission.DEPARTMENTS_READ)
  @ApiOperation({ summary: 'Get department detail' })
  @ApiParam({ name: 'id', description: 'Department identifier' })
  @ApiEnvelopeOkResponse(DepartmentResponseDto, 'Department detail')
  @ApiJwtErrorResponses({ notFound: 'Department was not found' })
  findOne(@Param('id') id: string) {
    return this.departmentsService.findOne(id);
  }

  @Patch(':id')
  @PermissionsDecorator(AppPermission.DEPARTMENTS_WRITE)
  @ApiOperation({ summary: 'Update department' })
  @ApiParam({ name: 'id', description: 'Department identifier' })
  @ApiEnvelopeOkResponse(DepartmentResponseDto, 'Department updated successfully')
  @ApiJwtErrorResponses({
    conflict: 'Department name or code already exists',
    notFound: 'Department was not found',
  })
  update(
    @Param('id') id: string,
    @Body() body: UpdateDepartmentDto,
    @CurrentUser() user: JwtAuthUser,
  ) {
    return this.departmentsService.update(id, body, user);
  }

  @Delete(':id')
  @PermissionsDecorator(AppPermission.DEPARTMENTS_WRITE)
  @ApiOperation({ summary: 'Soft delete department' })
  @ApiParam({ name: 'id', description: 'Department identifier' })
  @ApiEnvelopeOkResponse(DepartmentResponseDto, 'Department deleted successfully')
  @ApiJwtErrorResponses({ notFound: 'Department was not found' })
  remove(@Param('id') id: string, @CurrentUser() user: JwtAuthUser) {
    return this.departmentsService.remove(id, user);
  }

  @Patch(':id/restore')
  @PermissionsDecorator(AppPermission.DEPARTMENTS_WRITE)
  @ApiOperation({ summary: 'Restore soft-deleted department' })
  @ApiParam({ name: 'id', description: 'Department identifier' })
  @ApiEnvelopeOkResponse(DepartmentResponseDto, 'Department restored successfully')
  @ApiJwtErrorResponses({ notFound: 'Department was not found' })
  restore(@Param('id') id: string, @CurrentUser() user: JwtAuthUser) {
    return this.departmentsService.restore(id, user);
  }
}
