import { Controller, Get, Query, Res, UseGuards } from '@nestjs/common';
import { FastifyReply } from 'fastify';
import { ReportsService } from './reports.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('reports')
@UseGuards(JwtAuthGuard)
export class ReportsController {
    constructor(private readonly reportsService: ReportsService) { }

    @Get('orders/csv')
    async exportOrdersCsv(
        @Query('from') from: string,
        @Query('to') to: string,
        @Res() res: FastifyReply,
    ) {
        const csv = await this.reportsService.exportOrdersCsv(from, to);
        res.header('Content-Type', 'text/csv');
        res.header('Content-Disposition', `attachment; filename=orders_${from}_${to}.csv`);
        res.send(csv);
    }

    @Get('sellers/csv')
    async exportSellersCsv(@Res() res: FastifyReply) {
        const csv = await this.reportsService.exportSellersCsv();
        res.header('Content-Type', 'text/csv');
        res.header('Content-Disposition', 'attachment; filename=sellers.csv');
        res.send(csv);
    }

    @Get('ledger/csv')
    async exportLedgerCsv(
        @Query('from') from: string,
        @Query('to') to: string,
        @Res() res: FastifyReply,
    ) {
        const csv = await this.reportsService.exportLedgerCsv(from, to);
        res.header('Content-Type', 'text/csv');
        res.header('Content-Disposition', `attachment; filename=ledger_${from}_${to}.csv`);
        res.send(csv);
    }

    @Get('performance/csv')
    async exportPerformanceCsv(@Res() res: FastifyReply) {
        const csv = await this.reportsService.exportSellerPerformanceCsv();
        res.header('Content-Type', 'text/csv');
        res.header('Content-Disposition', 'attachment; filename=seller_performance.csv');
        res.send(csv);
    }
}

