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

import { BookingStaff } from '../../common/booking-guards';
import { FREIGHT_PERMS } from '../../seed/freight-permissions.registry';
import { GenerateFromScheduleDto } from './dto/generate-from-schedule.dto';
import { InterchangeDocumentQueryDto } from './dto/interchange-document-query.dto';
import {
  AcknowledgeInterchangeDocumentDto,
  DisputeInterchangeDocumentDto,
} from './dto/update-interchange-document-status.dto';
import { InterchangeDocumentsService } from './interchange-documents.service';

@ApiTags('interchange-documents')
@ApiBearerAuth()
@Controller('interchange-documents')
// Class-level view guard; each write route adds its own manage permission below.
@BookingStaff(FREIGHT_PERMS.interchangeDocuments.view)
export class InterchangeDocumentsController {
  constructor(private readonly service: InterchangeDocumentsService) {}

  @Get()
  @ApiOperation({ summary: 'List interchange documents' })
  findAll(@Query() query: InterchangeDocumentQueryDto) {
    return this.service.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get interchange document detail' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.service.findOne(id);
  }

  @Post('generate-from-schedule')
  @BookingStaff(FREIGHT_PERMS.interchangeDocuments.generate)
  @ApiOperation({ summary: 'Generate interchange document from a train schedule handover' })
  generateFromSchedule(@Body() dto: GenerateFromScheduleDto) {
    return this.service.generateFromSchedule(dto);
  }

  @Patch(':id/acknowledge')
  @BookingStaff(FREIGHT_PERMS.interchangeDocuments.acknowledge)
  @ApiOperation({ summary: 'Acknowledge an interchange document' })
  acknowledge(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: AcknowledgeInterchangeDocumentDto,
  ) {
    return this.service.acknowledge(id, dto);
  }

  @Patch(':id/dispute')
  @BookingStaff(FREIGHT_PERMS.interchangeDocuments.dispute)
  @ApiOperation({ summary: 'Dispute an interchange document' })
  dispute(@Param('id', ParseUUIDPipe) id: string, @Body() dto: DisputeInterchangeDocumentDto) {
    return this.service.dispute(id, dto);
  }
}
