import {
  Controller,
  Post,
  Param,
  Body,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';
import { AiScreeningService } from './ai_screening.service';
import { Public } from '../auth/public.decorator';

@ApiTags('AI_Screening')
@ApiBearerAuth('access_token')
@Controller('jobs')
export class AiScreeningController {
  constructor(private readonly aiScreeningService: AiScreeningService) {}

  /**
   * Screen and rank applicants for a specific job
   * POST /jobs/:jobId/ai-screening/applicants
   */
  @Public()
  @Post(':jobId/ai_screening/applicants')
  @ApiOperation({ summary: 'Screen Applicants' })
  @ApiParam({ name: 'jobId', type: Number })
  @ApiBody({
    schema: { type: 'object', properties: { weightage: { type: 'object' } } },
  })
  async screenApplicants(@Param('jobId') jobId: number, @Body() body: any) {
    try {
      const rankedCandidates = await this.aiScreeningService.screenApplicants(
        Number(jobId),
        body.weightage, // Optional weightage
      );

      return {
        success: true,
        message: `Successfully screened ${rankedCandidates.length} applicants`,
        data: {
          jobId: Number(jobId),
          totalCandidates: rankedCandidates.length,
          candidates: rankedCandidates,
        },
      };
    } catch (error) {
      throw new HttpException(
        {
          success: false,
          message: error.message || 'Failed to screen applicants',
          error: error.message,
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  /**
   * Find top candidates from database for a specific job
   * POST /jobs/:jobId/ai-screening/candidate-pool
   */
  @Public()
  @Post(':jobId/ai_screening/candidate-pool')
  @ApiOperation({ summary: 'Find Top Candidates' })
  @ApiParam({ name: 'jobId', type: Number })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        count: { type: 'number' },
        filters: { type: 'object' },
        weightage: { type: 'object' },
      },
    },
  })
  async findTopCandidates(
    @Param('jobId') jobId: number,
    @Body()
    body: {
      count?: number;
      filters?: {
        experience?: string;
        location?: string;
        skills?: string;
      };
      weightage?: any;
    },
  ) {
    try {
      const count = body.count || 10;
      const filters = body.filters || {};
      const weightage = body.weightage;

      // Validate count
      if (count < 1 || count > 100) {
        throw new HttpException(
          {
            success: false,
            message: 'Count must be between 1 and 100',
          },
          HttpStatus.BAD_REQUEST,
        );
      }

      const rankedCandidates = await this.aiScreeningService.findTopCandidates(
        Number(jobId),
        count,
        filters,
        weightage,
      );

      return {
        success: true,
        message: `Found ${rankedCandidates.length} top candidates`,
        data: {
          jobId: Number(jobId),
          requestedCount: count,
          totalCandidates: rankedCandidates.length,
          filters,
          candidates: rankedCandidates,
        },
      };
    } catch (error) {
      throw new HttpException(
        {
          success: false,
          message: error.message || 'Failed to find candidates',
          error: error.message,
        },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}
