import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Post,
  Query,
} from '@nestjs/common';
import { CurrentUser } from '@edr/api-common';
import type { TCurrentUser } from '@tria-plc/api-common/modules/auth/types/current-user.type';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { BookingOrdersService } from './booking-orders.service';
import { CreateBookingOrderDto } from './dto/create-booking-order.dto';
import { GeneralContractService } from './general-contract.service';

@ApiTags('Booking Orders')
@Controller('booking-orders')
export class BookingOrdersController {
  constructor(
    private readonly ordersService: BookingOrdersService,
    private readonly generalContractService: GeneralContractService,
  ) {}

  @Post()
  @ApiOperation({ summary: 'Place a drawdown order against a general contract' })
  async create(
    @Body() dto: CreateBookingOrderDto,
    @CurrentUser() user: TCurrentUser,
  ) {
    return this.ordersService.create(dto, user?.id);
  }

  @Get()
  @ApiOperation({ summary: 'List orders placed against a contract' })
  async list(@Query('contractBookingId', ParseUUIDPipe) contractBookingId: string) {
    return this.ordersService.listByContract(contractBookingId);
  }

  @Get('contract/:id/pool')
  @ApiOperation({
    summary: 'Contracted / ordered / remaining quantities for a general contract',
  })
  async pool(@Param('id', ParseUUIDPipe) id: string) {
    return this.generalContractService.getQuantityLines(id);
  }

  @Get('contract/:id/routes')
  @ApiOperation({
    summary:
      'Per-route contracted / ordered / remaining quantities (multi-route contracts). Empty for single-route.',
  })
  async routes(@Param('id', ParseUUIDPipe) id: string) {
    return this.generalContractService.getRouteLines(id);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get a single booking order' })
  async findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.ordersService.findById(id);
  }
}
