import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { ApplicantsService } from './applicants.service';
import { CreateApplicantDto, UpdateApplicantDto, ApplicantResponseDto } from './dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Roles } from '../../common/decorators';
import { RolesGuard } from '../../common/guards';
import { UserRole } from '../../common/enums';

@ApiTags('Applicants')
@Controller('applicants')
@UseGuards(JwtAuthGuard, RolesGuard)
export class ApplicantsController {
  constructor(private readonly applicantsService: ApplicantsService) {}

  @Get()
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth('BearerAuth')
  @ApiOperation({ summary: 'List all applicants (Admin only)' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiResponse({ status: 200, description: 'List of applicants' })
  async findAll(@Query('page') page?: number, @Query('limit') limit?: number) {
    return this.applicantsService.findAll({ page, limit });
  }

  @Get(':id')
  @Roles(UserRole.ADMIN, UserRole.APPLICANT)
  @ApiBearerAuth('BearerAuth')
  @ApiOperation({ summary: 'Get applicant by ID' })
  @ApiResponse({ status: 200, description: 'Applicant details', type: ApplicantResponseDto })
  @ApiResponse({ status: 404, description: 'Applicant not found' })
  async findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.applicantsService.findById(id);
  }

  @Get(':id/stats')
  @Roles(UserRole.ADMIN, UserRole.APPLICANT)
  @ApiBearerAuth('BearerAuth')
  @ApiOperation({ summary: 'Get applicant statistics' })
  @ApiResponse({
    status: 200,
    description: 'Applicant statistics',
    schema: {
      properties: {
        totalRequests: { type: 'number' },
        pendingRequests: { type: 'number' },
        approvedLicenses: { type: 'number' },
        documentsUploaded: { type: 'number' },
        blockchainRecords: { type: 'number' },
      },
    },
  })
  @ApiResponse({ status: 404, description: 'Applicant not found' })
  async getStats(@Param('id', ParseUUIDPipe) id: string) {
    return this.applicantsService.getStats(id);
  }

  @Post()
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth('BearerAuth')
  @ApiOperation({ summary: 'Create new applicant (Admin only)' })
  @ApiResponse({ status: 201, description: 'Applicant created', type: ApplicantResponseDto })
  @ApiResponse({ status: 409, description: 'Applicant already exists' })
  async create(@Body() dto: CreateApplicantDto) {
    return this.applicantsService.create(dto);
  }

  @Put(':id')
  @Roles(UserRole.ADMIN, UserRole.APPLICANT)
  @ApiBearerAuth('BearerAuth')
  @ApiOperation({ summary: 'Update applicant' })
  @ApiResponse({ status: 200, description: 'Applicant updated', type: ApplicantResponseDto })
  @ApiResponse({ status: 404, description: 'Applicant not found' })
  async update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateApplicantDto) {
    return this.applicantsService.update(id, dto);
  }
}
