import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  Res,
  UseGuards,
  ParseUUIDPipe,
} from '@nestjs/common';
import type { Response } from 'express';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { ReportsService } from './reports.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { ModuleGuard } from '../../common/guards/module.guard';
import { Roles, UserRole } from '../../common/decorators/roles.decorator';
import { RequireModule } from '../../common/decorators/require-module.decorator';
import {
  CurrentUser,
  JwtPayload,
} from '../../common/decorators/current-user.decorator';
import { GenerateReportDto, FindReportsDto } from './dto';

@ApiTags('Reports')
@ApiBearerAuth()
@Controller('reports')
@UseGuards(JwtAuthGuard, ModuleGuard)
@RequireModule('web_reports')
export class ReportsController {
  constructor(private readonly reportsService: ReportsService) {}

  @Get()
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER, UserRole.RECEPTIONIST, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'List reports with pagination' })
  @ApiResponse({ status: 200, description: 'Paginated list of reports' })
  async findAll(
    @CurrentUser() user: JwtPayload,
    @Query() query: FindReportsDto,
  ) {
    return this.reportsService.findAll(user.clubId, query);
  }

  @Get(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER, UserRole.RECEPTIONIST, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Get report by ID' })
  @ApiParam({ name: 'id', description: 'Report UUID' })
  @ApiResponse({ status: 200, description: 'Report details' })
  @ApiResponse({ status: 404, description: 'Report not found' })
  async findById(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.reportsService.findById(user.clubId, id);
  }

  @Post('generate')
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER, UserRole.RECEPTIONIST, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Generate a new report' })
  @ApiResponse({ status: 201, description: 'Report generation started' })
  async generate(
    @CurrentUser() user: JwtPayload,
    @Body() dto: GenerateReportDto,
  ) {
    return this.reportsService.generate(user.clubId, user.sub, dto);
  }

  @Get(':id/download')
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER, UserRole.RECEPTIONIST, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Get report download URL' })
  @ApiParam({ name: 'id', description: 'Report UUID' })
  @ApiResponse({ status: 200, description: 'File download URL' })
  @ApiResponse({ status: 404, description: 'Report not found or not ready' })
  async download(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.reportsService.getDownloadUrl(user.clubId, id);
  }

  @Get(':id/file')
  @UseGuards(RolesGuard)
  @Roles(UserRole.MANAGER, UserRole.RECEPTIONIST, UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Download report PDF file' })
  @ApiParam({ name: 'id', description: 'Report UUID' })
  @ApiResponse({ status: 200, description: 'PDF file' })
  @ApiResponse({ status: 404, description: 'Report not found or not ready' })
  async getFile(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Res() res: Response,
  ) {
    const filePath = await this.reportsService.getFile(user.clubId, id);
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', `attachment; filename="report-${id}.pdf"`);
    res.sendFile(filePath);
  }
}
