import { Body, Controller, Get, Param, Post, Query } from "@nestjs/common";
import { ApiTags, ApiOperation, ApiBearerAuth } from "@nestjs/swagger";
import { ReportsService } from "./reports.service";
import { GenerateReportDto } from "./reports.dto";
import { PassengerStaff } from "../../common/passenger-guards";
import { PASSENGER_PERMS } from "../../seed/passenger-permissions.registry";

@ApiTags("Reports")
@Controller("reports")
@PassengerStaff([PASSENGER_PERMS.reports.view, PASSENGER_PERMS.admin])
@ApiBearerAuth("IAM-auth")
export class ReportsController {
  constructor(private service: ReportsService) {}

  @Post("generate")
  @ApiOperation({ summary: "Generate operational report" })
  generateReport(@Body() dto: GenerateReportDto) {
    return this.service.generateReport(dto);
  }

  @Get('schedules')
  @ApiOperation({ summary: 'List schedules for the passengers report picker' })
  listSchedulesForPicker(@Query('all') all?: string) {
    return this.service.listSchedulesForPicker(all === 'true');
  }

  @Get("passengers/list")
  @ApiOperation({ summary: "Flat passenger list for a specific schedule" })
  getPassengerList(@Query("scheduleId") scheduleId: string) {
    return this.service.getPassengerList(scheduleId);
  }

  @Get("passengers")
  @ApiOperation({ summary: "Passengers report for a specific schedule" })
  getOccupancyReport(@Query("scheduleId") scheduleId: string) {
    return this.service.getOccupancyBySchedule(scheduleId);
  }

  @Get("payment-discrepancy")
  @ApiOperation({ summary: "Payment discrepancy report — bookings where paid amount is less than the fare. Pass `search` to look up a specific PNR or ticket number." })
  getPaymentDiscrepancy(
    @Query('from') from?: string,
    @Query('to') to?: string,
    @Query('sortBy') sortBy?: string,
    @Query('search') search?: string,
  ) {
    return this.service.getPaymentDiscrepancyReport({ from, to, sortBy, search });
  }

  @Get("seat-status")
  @ApiOperation({ summary: "Seat status breakdown for a schedule (paid, unpaid, expired holds, blocked)" })
  getSeatStatusReport(@Query('scheduleId') scheduleId: string) {
    return this.service.getSeatStatusReport(scheduleId);
  }

  @Get("boarding")
  @ApiOperation({ summary: "Boarding report for a schedule — boarded vs not-boarded passengers" })
  getBoardingReport(@Query('scheduleId') scheduleId: string) {
    return this.service.getBoardingReport(scheduleId);
  }

  @Get("payments")
  @ApiOperation({ summary: "Payments collected for a schedule" })
  getPaymentsReport(@Query('scheduleId') scheduleId: string) {
    return this.service.getPaymentsReport(scheduleId);
  }

  @Get("payments/discrepancy")
  @ApiOperation({ summary: "Payment discrepancy breakdown for a schedule" })
  getPaymentDiscrepancyBySchedule(
    @Query('scheduleId') scheduleId: string,
    @Query('search') search?: string,
    @Query('seatClass') seatClass?: string,
    @Query('sort') sort?: string,
  ) {
    return this.service.getPaymentDiscrepancyBySchedule(scheduleId, { search, seatClass, sort });
  }

  @Get(":reportId")
  @ApiOperation({ summary: "Get report by ID" })
  getReport(@Param("reportId") reportId: string) {
    return this.service.getReport(reportId);
  }

  @Get()
  @ApiOperation({ summary: "List reports" })
  listReports(@Query("type") type?: string) {
    return this.service.listReports(type);
  }
}
