import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { TicketsService } from './tickets.service';
import { JwtGuard } from '../../common/jwt.guard';

@ApiTags('Tickets')
@Controller('tickets')
@UseGuards(JwtGuard)
@ApiBearerAuth('JWT-auth')
export class TicketsController {
  constructor(private service: TicketsService) {}
  
  @Get(':bookingRef')
  @ApiOperation({ 
    summary: 'Get ticket with QR code and passenger details',
    description: `Returns ticket information including:
- QR code for gate scanning
- Barcode for offline validation
- Passenger details (name, age category, nationality)
- Journey details (origin, destination, seat, coach)
- Fare breakdown with currency
- PDF download link`
  })
  getByRef(@Param('bookingRef') ref: string) { 
    return this.service.getByRef(ref); 
  }
  
  @Post(':bookingRef/validate')
  @ApiOperation({ 
    summary: 'Validate ticket at gate with audit logging',
    description: 'Validates ticket QR/barcode at station gate. Records validation in audit log with timestamp, gate, and validator.'
  })
  validate(
    @Param('bookingRef') ref: string, 
    @Body('validatorId') validatorId: string,
    @Body('gateId') gateId?: string
  ) { 
    return this.service.validate(ref, validatorId, gateId); 
  }
  
  @Get(':ticketId/validation-logs')
  @ApiOperation({ summary: 'Get validation logs for ticket' })
  getValidationLogs(@Param('ticketId') ticketId: string) {
    return this.service.getValidationLogs(ticketId);
  }

  @Get('offline/export')
  @ApiOperation({ summary: 'Export tickets for offline validation' })
  exportOfflineData(@Query('scheduleId') scheduleId: string) {
    return this.service.exportOfflineData(scheduleId);
  }

  @Post('validate/offline')
  @ApiOperation({ summary: 'Batch import offline validations' })
  validateOfflineBatch(@Body() body: { validations: any[] }) {
    return this.service.validateOfflineBatch(body.validations);
  }
}
