import {
  Controller, Get, Query,
  UseGuards, Res, Req
} from '@nestjs/common';
import type { Response } from 'express';
import { ReportsService } from './reports.service';
import { ReportFiltersDto } from './dto/report-filters.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles, Role } from '../common/decorators/roles.decorator';
import { ReportsDashboardService } from './reports-dashboard.service';
import dayjs from 'dayjs';

@Controller('reports')
@UseGuards(JwtAuthGuard)
export class ReportsController {
  constructor(
    private readonly reportsService: ReportsService,
    private readonly dashboardService: ReportsDashboardService,
  ) {}

  // Export Excel congés
  @Get('leaves/excel')
  @UseGuards(RolesGuard)
  @Roles(Role.AGENCY_MANAGER, Role.DIRECTOR)
  async leavesExcel(@Query() filters: ReportFiltersDto, @Res() res: Response) {
    const buffer   = await this.reportsService.exportLeavesExcel(filters);
    const filename = `conges-${dayjs().format('YYYY-MM-DD')}.xlsx`;

    res.set({
      'Content-Type':        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': `attachment; filename="${filename}"`,
      'Content-Length':      buffer.byteLength,
    });
    res.end(buffer);
  }

  // Export CSV congés
  @Get('leaves/csv')
  @UseGuards(RolesGuard)
  @Roles(Role.AGENCY_MANAGER, Role.DIRECTOR)
  async leavesCSV(@Query() filters: ReportFiltersDto, @Res() res: Response) {
    const csv      = await this.reportsService.exportLeavesCSV(filters);
    const filename = `conges-${dayjs().format('YYYY-MM-DD')}.csv`;

    res.set({
      'Content-Type':        'text/csv; charset=utf-8',
      'Content-Disposition': `attachment; filename="${filename}"`,
    });
    res.end(csv);
  }

  // Export Excel soldes
  @Get('balances/excel')
  @UseGuards(RolesGuard)
  @Roles(Role.AGENCY_MANAGER, Role.DIRECTOR)
  async balancesExcel(@Query() filters: ReportFiltersDto, @Res() res: Response) {
    const buffer   = await this.reportsService.exportBalancesExcel(filters);
    const filename = `soldes-${dayjs().format('YYYY')}.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 résumé RH
  @Get('summary/pdf')
  @UseGuards(RolesGuard)
  @Roles(Role.AGENCY_MANAGER, Role.DIRECTOR)
  async summaryPDF(@Query() filters: ReportFiltersDto, @Res() res: Response) {
    const buffer   = await this.reportsService.exportSummaryPDF(filters);
    const filename = `resume-rh-${dayjs().format('YYYY-MM-DD')}.pdf`;

    res.set({
      'Content-Type':        'application/pdf',
      'Content-Disposition': `attachment; filename="${filename}"`,
      'Content-Length':      buffer.length,
    });
    res.end(buffer);
  }

  // Dashboard KPIs
  @Get('dashboard')
  getDashboard(@Req() req) {
    return this.dashboardService.getDashboardKPIs(req.user);
  }
}