import { Roles } from '@/common/decorators/roles.decorator';
import { PaginationDto } from '@/common/dto/pagination.dto';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Req,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CreateSurveyDto } from './dto/create-survey.dto';
import { UpdateSurveyDto } from './dto/update-survey.dto';
import { SurveysService } from './surveys.service';
import { AnswersSurveyDto } from './dto/response-survey.dto';
import { Request } from 'express';
import { FindForUserDto } from './dto/find-for-user.dto';
import { SurveyStatisticsResponseDto } from './dto/statistics-response.dto';

@ApiTags('surveys')
@Controller('surveys')
export class SurveysController {
  constructor(private readonly surveysService: SurveysService) {}

  @Get()
  @Roles('admin')
  @ApiOperation({ summary: 'Get all surveys with pagination and filters' })
  @ApiResponse({ status: 200, description: 'Return paginated surveys.' })
  async findAll(@Query() paginationDto: PaginationDto) {
    const result = await this.surveysService.findAll(paginationDto);
    return {
      message: 'Surveys fetched successfully',
      data: result.data,
      meta: result.meta,
    };
  }



  @Post('for-user')
  @ApiOperation({ summary: 'Get all surveys with pagination and filters for user' })
  @ApiResponse({ status: 200, description: 'Return paginated surveys for user.' })
  async findAllForUser(@Req() req: Request, @Query() paginationDto: PaginationDto, @Body() findForUserDto: FindForUserDto) {
    
    const userId = req['user'].id;
    
    const result = await this.surveysService.findAllForUser(paginationDto, userId, findForUserDto);

    return {
      message: 'Surveys fetched successfully',
      data: result.data,
      meta: result.meta,
    };
  }

  @Get('statistics')
  @Roles('admin', 'superadmin', 'autoridad')
  @ApiOperation({ summary: 'Get survey statistics' })

  @ApiResponse({ status: 200, description: 'Return survey statistics.'})

  async getStatistics() {
    const data = await this.surveysService.getStatistics();
    return { 
      message: 'Survey statistics fetched successfully', 
      data
    };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get a survey by ID' })
  @ApiResponse({ status: 200, description: 'Return the survey.' })
  @ApiResponse({ status: 404, description: 'Survey not found.' })
  async findOne(@Param('id') id: string) {
    const data = await this.surveysService.findOne(id);
    return { message: 'Survey fetched successfully', data };
  }

  @Post()
  @Roles('admin')
  @ApiOperation({ summary: 'Create a new survey' })
  @ApiResponse({ status: 201, description: 'Survey created successfully.' })
  async create(@Body() createSurveyDto: CreateSurveyDto) {
    console.log(createSurveyDto);
    
    const data = await this.surveysService.create(createSurveyDto);
    return { message: 'Survey created successfully', data };
  }

  @Patch(':id')
  @Roles('admin')
  @ApiOperation({ summary: 'Update a survey' })
  @ApiResponse({ status: 200, description: 'Survey updated successfully.' })
  @ApiResponse({ status: 404, description: 'Survey not found.' })
  async update(
    @Param('id') id: string,
    @Body() updateSurveyDto: UpdateSurveyDto,
  ) {
    const data = await this.surveysService.update(id, updateSurveyDto);
    return { message: 'Survey updated successfully', data };
  }

  @Delete(':id')
  @Roles('admin')
  @ApiOperation({ summary: 'Delete a survey' })
  @ApiResponse({ status: 200, description: 'Survey deleted successfully.' })
  @ApiResponse({ status: 404, description: 'Survey not found.' })
  async remove(@Param('id') id: string) {
    return await this.surveysService.remove(id);
  }

  @Post('answers')
  @ApiOperation({ summary: 'Create a new answers' })
  @ApiResponse({ status: 201, description: 'Survey answers successfully.' })
  async answers(@Req() req: Request, @Body() answersSurveyDto: AnswersSurveyDto) {
    const userId = (req as any).user?.id;
    const data = await this.surveysService.answers(Number(userId),answersSurveyDto);
    return { message: 'Survey answers created successfully', data };
  }

 
}