import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Patch,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { Public } from '../auth/public.decorator';
import { MockTemplatesService } from './mock_templates.service';
import {
  ApiTags,
  ApiOperation,
  ApiQuery,
  ApiBody,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('mock_templates')
@Controller('admin/mock-templates')
export class MockTemplatesController {
  constructor(private readonly mockTemplatesService: MockTemplatesService) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all mock templates' })
  @ApiQuery({ name: 'role_id', required: false, type: Number })
  async findAll(@Query('role_id') roleId?: number) {
    return await this.mockTemplatesService.findAll(
      roleId ? Number(roleId) : undefined,
    );
  }

  @Public()
  @Get(':id')
  @ApiOperation({ summary: 'Get mock template by ID' })
  @ApiParam({ name: 'id', type: Number })
  async findOne(@Param('id') id: number) {
    return await this.mockTemplatesService.findOne(Number(id));
  }

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create mock template' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        role_id: { type: 'number', description: 'Role ID' },
        title: { type: 'string', description: 'Template title' },
        total_questions: {
          type: 'number',
          description: 'Total questions count',
        },
        duration_minutes: {
          type: 'number',
          description: 'Duration in minutes',
        },
        allowed_difficulty: {
          type: 'array',
          items: { type: 'string' },
          description: 'Allowed difficulty levels (easy, medium, hard)',
        },
        required_skill_ids: {
          type: 'array',
          items: { type: 'number' },
          description: 'Required skill IDs',
        },
        is_active: { type: 'boolean', description: 'Is template active' },
        created_by: { type: 'number', description: 'Admin user ID' },
      },
      required: ['role_id', 'title'],
    },
  })
  async create(@Body() data: any) {
    return await this.mockTemplatesService.create(data);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update mock template' })
  @ApiParam({ name: 'id', type: Number })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        role_id: { type: 'number' },
        title: { type: 'string' },
        total_questions: { type: 'number' },
        duration_minutes: { type: 'number' },
        allowed_difficulty: { type: 'array', items: { type: 'string' } },
        required_skill_ids: { type: 'array', items: { type: 'number' } },
        is_active: { type: 'boolean' },
      },
    },
  })
  async update(@Param('id') id: number, @Body() data: any) {
    return await this.mockTemplatesService.update(Number(id), data);
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete mock template' })
  @ApiParam({ name: 'id', type: Number })
  async delete(@Param('id') id: number) {
    return await this.mockTemplatesService.delete(Number(id));
  }

  @Public()
  @Patch(':id/toggle')
  @ApiOperation({ summary: 'Toggle mock template active status' })
  @ApiParam({ name: 'id', type: Number })
  async toggleStatus(@Param('id') id: number) {
    return await this.mockTemplatesService.toggleStatus(Number(id));
  }
}
