import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseIntPipe,
  DefaultValuePipe,
} from '@nestjs/common';
import { QuestionBankService } from './question_bank.service';
import { QuestionBank } from './entities/question_bank.entity';
import { Public } from '../auth/public.decorator';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiBody,
  ApiQuery,
} from '@nestjs/swagger';

@ApiTags('question_bank')
@ApiBearerAuth('access_token')
@Controller('question_bank')
export class QuestionBankController {
  constructor(private readonly questionBankService: QuestionBankService) {}

  @Public()
  @Get('role/:roleId')
  @ApiOperation({ summary: 'Get questions by role ID with pagination' })
  @ApiParam({ name: 'roleId', type: Number })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getQuestionsByRole(
    @Param('roleId', ParseIntPipe) roleId: number,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number,
  ) {
    return this.questionBankService.findByRole(roleId, page, limit);
  }

  @Public()
  @Post('generate-mock')
  @ApiOperation({ summary: 'Generate Mock Test Questions' })
  @ApiBody({
    schema: {
      example: {
        roleId: 1,
        roleName: 'React Developer',
        count: 10,
        difficulty: 'Medium',
        skills: ['React', 'TypeScript'],
        experienceLevel: 'Mid Level',
      },
    },
  })
  async generateMockQuestions(@Body() body: any) {
    return this.questionBankService.generateAndSaveMockTestQuestions(
      body.roleId,
      body.roleName,
      body.count,
      body.difficulty,
      body.skills,
      body.experienceLevel,
    );
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update a question' })
  @ApiParam({ name: 'id', type: Number })
  async updateQuestion(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: any,
  ) {
    return this.questionBankService.updateQuestion(id, body);
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete a question' })
  @ApiParam({ name: 'id', type: Number })
  async deleteQuestion(@Param('id', ParseIntPipe) id: number) {
    return this.questionBankService.deleteQuestion(id);
  }
}
