import { Body, Controller, Get, Param, ParseUUIDPipe, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';

import { GenerateInvoiceDto, PayInvoiceBodyDto } from './dto/invoice.dto';
import { WarehouseInvoiceService } from './warehouse-invoice.service';

@ApiTags('warehouse-fee-invoices')
@ApiBearerAuth()
@Controller()
export class WarehouseInvoiceController {
  constructor(private readonly invoiceService: WarehouseInvoiceService) {}

  @Post('warehouse-inventory/:id/generate-fee-invoice')
  @ApiOperation({ summary: 'Generate a warehouse fee invoice from Batch 5 fee calculation' })
  generate(@Param('id', ParseUUIDPipe) id: string, @Body() dto: GenerateInvoiceDto) {
    return this.invoiceService.generateForInventory(id, dto);
  }

  @Get('warehouse-inventory/:id/fee-invoices')
  @ApiOperation({ summary: 'List fee invoices for an inventory item' })
  listForInventory(@Param('id', ParseUUIDPipe) id: string) {
    return this.invoiceService.listForInventory(id);
  }

  @Get('bookings/:id/warehouse-fee-invoices')
  @ApiOperation({ summary: 'List warehouse fee invoices for a booking' })
  listForBooking(@Param('id', ParseUUIDPipe) id: string) {
    return this.invoiceService.listForBooking(id);
  }

  @Get('warehouse-fee-invoices')
  @ApiOperation({ summary: 'List / filter warehouse fee invoices' })
  findAll(
    @Query('status') status?: string,
    @Query('invoiceType') invoiceType?: string,
    @Query('warehouseId') warehouseId?: string,
    @Query('facilityId') facilityId?: string,
    @Query('customerId') customerId?: string,
    @Query('bookingId') bookingId?: string,
  ) {
    return this.invoiceService.findAll({
      status: status as never,
      invoiceType: invoiceType as never,
      warehouseId,
      facilityId,
      customerId,
      bookingId,
    });
  }

  @Get('warehouse-fee-invoices/:id')
  @ApiOperation({ summary: 'Get a warehouse fee invoice with items + payment history' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.invoiceService.findById(id);
  }

  @Patch('warehouse-fee-invoices/:id/cancel')
  @ApiOperation({ summary: 'Cancel a warehouse fee invoice' })
  cancel(@Param('id', ParseUUIDPipe) id: string) {
    return this.invoiceService.cancel(id);
  }

  @Post('warehouse-fee-invoices/:id/pay')
  @ApiOperation({ summary: 'Record a payment against a warehouse fee invoice' })
  pay(@Param('id', ParseUUIDPipe) id: string, @Body() dto: PayInvoiceBodyDto) {
    return this.invoiceService.pay(id, dto);
  }
}
