import {
  Controller, Get, Param,
  Query, UseGuards, Res
} from '@nestjs/common';
import { PlanningService } from './planning.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import type { Response } from 'express';
import { PlanningExportService } from './planning-export.service';

@Controller('planning')
@UseGuards(JwtAuthGuard)
export class PlanningController {
  constructor(
    private readonly planningService: PlanningService,
    private readonly exportService: PlanningExportService,
  ) {}

  // Planning équipe — vue mensuelle ou hebdomadaire
  @Get('team/:teamId')
  getTeamPlanning(
    @Param('teamId') teamId: string,
    @Query('view') view: 'month' | 'week' = 'month',
    @Query('date') date: string = new Date().toISOString().split('T')[0],
  ) {
    return this.planningService.getTeamPlanning(+teamId, view, date);
  }

  // Vérifier le seuil minimum de présence
  @Get('team/:teamId/threshold')
  checkThreshold(
    @Param('teamId') teamId: string,
    @Query('start_date') startDate: string,
    @Query('end_date') endDate: string,
  ) {
    return this.planningService.checkPresenceThreshold(+teamId, startDate, endDate);
  }

  // Planning global — tous les salariés
  @Get('global')
  getGlobalPlanning(
    @Query('view') view: 'month' | 'week' = 'month',
    @Query('date') date: string = new Date().toISOString().split('T')[0],
    @Query('team_id') teamId?: string,
    @Query('search') search?: string,
    @Query('page') page: string = '1',
    @Query('limit') limit: string = '20',
  ) {
    return this.planningService.getGlobalPlanning(
      view,
      date,
      teamId ? +teamId : undefined,
      search,
      +page,
      +limit,
    );
  }

  // Export Excel planning équipe
  @Get('team/:teamId/export/excel')
  async exportTeamExcel(
    @Param('teamId') teamId: string,
    @Query('view') view: 'month' | 'week' = 'month',
    @Query('date') date: string = new Date().toISOString().split('T')[0],
    @Res() res: Response,
  ) {
    const buffer = await this.exportService.exportTeamPlanningExcel(+teamId, view, date);
    const filename = `planning-equipe-${teamId}-${date}.xlsx`;

    res.set({
      'Content-Type':        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': `attachment; filename="${filename}"`,
      'Content-Length':      buffer.byteLength,
    });

    res.end(buffer);
  }

  // Export PDF planning équipe
  @Get('team/:teamId/export/pdf')
  async exportTeamPDF(
    @Param('teamId') teamId: string,
    @Query('view') view: 'month' | 'week' = 'month',
    @Query('date') date: string = new Date().toISOString().split('T')[0],
    @Res() res: Response,
  ) {
    const buffer   = await this.exportService.exportTeamPlanningPDF(+teamId, view, date);
    const filename = `planning-equipe-${teamId}-${date}.pdf`;

    res.set({
      'Content-Type':        'application/pdf',
      'Content-Disposition': `attachment; filename="${filename}"`,
      'Content-Length':      buffer.length,
    });

    res.end(buffer);
  }

  // Export Excel planning global
  @Get('global/export/excel')
  async exportGlobalExcel(
    @Query('view') view: 'month' | 'week' = 'month',
    @Query('date') date: string = new Date().toISOString().split('T')[0],
    @Res() res: Response,
  ) {
    const buffer   = await this.exportService.exportGlobalPlanningExcel(view, date);
    const filename = `planning-global-${date}.xlsx`;

    res.set({
      'Content-Type':        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': `attachment; filename="${filename}"`,
      'Content-Length':      buffer.byteLength,
    });

    res.end(buffer);
  }
}