import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Param,
  Query,
  UnauthorizedException,
} from '@nestjs/common';
import { Public } from '../auth/public.decorator';
import { AiInterviewSchedulesService } from './ai_interview_schedules.service';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('ai_interview_schedules')
@ApiBearerAuth('access_token')
@Controller('ai_interview_schedules')
export class AiInterviewSchedulesController {
  constructor(private readonly scheduleService: AiInterviewSchedulesService) {}

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Schedule' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        application_id: { type: 'number' },
        seeker_id: { type: 'number' },
        job_id: { type: 'number' },
        requester_user_id: { type: 'number' },
        scheduled_at: { type: 'string', format: 'date-time' },
        mode: { type: 'string', enum: ['instant', 'scheduled'] },
      },
      required: ['application_id', 'seeker_id', 'job_id', 'requester_user_id'],
    },
  })
  async create(@Body() data: any) {
    return await this.scheduleService.createSchedule(data);
  }

  @Public()
  @Get('token/:token')
  async getByToken(@Param('token') token: string) {
    return await this.scheduleService.getScheduleByToken(token);
  }

  @Public()
  @Get('seeker/:seekerId')
  async getSeekerSchedules(@Param('seekerId') seekerId: string) {
    const seekerIdNum = parseInt(seekerId);
    return await this.scheduleService.getSeekerSchedules(seekerIdNum);
  }

  @Public()
  @Get('job/:jobId')
  async getJobSchedules(@Param('jobId') jobId: string) {
    const jobIdNum = parseInt(jobId);
    return await this.scheduleService.getJobSchedules(jobIdNum);
  }

  @Public()
  @Put(':id/start')
  @ApiOperation({ summary: 'Start Interview' })
  @ApiParam({ name: 'id', type: String })
  async startInterview(@Param('id') id: string) {
    const idNum = parseInt(id);
    return await this.scheduleService.startInterview(idNum);
  }

  @Public()
  @Put(':id/complete')
  @ApiOperation({ summary: 'Complete Interview' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        submission_type: { type: 'string' },
        reason: { type: 'string' },
      },
    },
  })
  async completeInterview(
    @Param('id') id: string,
    @Body() body: { submission_type?: string; reason?: string },
  ) {
    const idNum = parseInt(id);
    return await this.scheduleService.completeInterview(idNum, body);
  }

  @Public()
  @Put(':id/no-show')
  async markNoShow(@Param('id') id: string) {
    const idNum = parseInt(id);
    return await this.scheduleService.markNoShow(idNum);
  }

  @Public()
  @Put(':id/answer')
  @ApiOperation({ summary: 'Submit Answer' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        questionId: { type: 'number' },
        answer: { type: 'object' },
      },
      required: ['questionId', 'answer'],
    },
  })
  async submitAnswer(
    @Param('id') id: string,
    @Body() body: { questionId: number; answer: any },
  ) {
    const idNum = parseInt(id);
    return await this.scheduleService.submitAnswer(
      idNum,
      body.questionId,
      body.answer,
    );
  }

  @Public()
  @Put(':id/answers-bulk')
  @ApiOperation({ summary: 'Submit Bulk Answers' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        answers: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              questionId: { type: 'number' },
              answer: { type: 'object' },
            },
          },
        },
      },
      required: ['answers'],
    },
  })
  async submitBulkAnswers(
    @Param('id') id: string,
    @Body() body: { answers: { questionId: number; answer: any }[] },
  ) {
    const idNum = parseInt(id);
    return await this.scheduleService.submitBulkAnswers(idNum, body.answers);
  }

  @Public()
  @Put(':id/reschedule')
  @ApiOperation({ summary: 'Reschedule Interview' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        scheduled_at: { type: 'string', format: 'date-time' },
      },
      required: ['scheduled_at'],
    },
  })
  async reschedule(@Param('id') id: string, @Body() data: any) {
    const idNum = parseInt(id);
    return await this.scheduleService.updateSchedule(idNum, data);
  }

  @Public()
  @Get(':id')
  async getById(@Param('id') id: string) {
    const idNum = parseInt(id);
    return await this.scheduleService.getScheduleById(idNum);
  }
}
