import { IsString, IsArray, ValidateNested, IsOptional, IsInt, IsEnum, IsDateString } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Currency, IdDocumentType } from '@prisma/client';

export class PassengerInputDto {
  @ApiProperty({ example: 'seat-uuid', description: 'Outbound / leg-1 seat ID (all booking types)' }) @IsString() seatId: string;
  @ApiPropertyOptional({ example: 'leg2-seat-uuid', description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Outbound leg-2 seat ID' }) @IsOptional() @IsString() leg2SeatId?: string;
  @ApiPropertyOptional({ example: 'return-seat-uuid', description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return leg-1 seat ID' }) @IsOptional() @IsString() returnSeatId?: string;
  @ApiPropertyOptional({ example: 'ret-leg2-seat-uuid', description: '**ROUND_TRIP_TRANSIT:** Return leg-2 seat ID' }) @IsOptional() @IsString() returnLeg2SeatId?: string;
  @ApiProperty({ example: 'Abebe Kebede' }) @IsString() passengerName: string;
  @ApiProperty({ example: '1990-05-15', description: 'Date of birth (YYYY-MM-DD) for age calculation. Age <5 = CHILD (first free), Age ≥5 = ADULT (full fare)' }) @IsDateString() dateOfBirth: string;
  @ApiProperty({ example: 'NATIONAL_ID', enum: IdDocumentType, description: 'NATIONAL_ID for Ethiopians (Verifayda verified), PASSPORT for others' }) @IsEnum(IdDocumentType) idDocumentType: IdDocumentType;
  @ApiPropertyOptional({ example: 'ET123456789', description: 'Ethiopian national ID - verified via Verifayda 2.0 (NOT stored in database)' }) @IsOptional() @IsString() idDocumentNumber?: string;
  @ApiPropertyOptional({ example: 'P1234567', description: 'Passport number for non-Ethiopian passengers (no verification)' }) @IsOptional() @IsString() passportNumber?: string;
  @ApiPropertyOptional({ example: 'Djibouti', description: 'Passport issuing country for non-Ethiopians' }) @IsOptional() @IsString() passportCountry?: string;
  @ApiPropertyOptional({ example: 'Ethiopian', description: 'Ethiopian (Verifayda + Telebirr/CBE/eBirr), Djiboutian (Passport + Waafi), Other (Passport + Card)' }) @IsOptional() @IsString() nationality?: string;
}

export class RoundTripPassengerDto {
  @ApiProperty({ description: 'Outbound journey seat ID', example: 'seat-uuid-outbound' }) 
  @IsString() outboundSeatId: string;
  
  @ApiProperty({ description: 'Return journey seat ID', example: 'seat-uuid-return' }) 
  @IsString() returnSeatId: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Outbound leg-2 seat ID' })
  @IsOptional() @IsString() outboundLeg2SeatId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-2 seat ID' })
  @IsOptional() @IsString() returnLeg2SeatId?: string;
  
  @ApiProperty({ 
    example: 'Abebe Kebede',
    description: 'Full passenger name (will be verified via Verifayda for Ethiopian nationals)'
  }) 
  @IsString() passengerName: string;
  
  @ApiProperty({ 
    example: '1990-05-15', 
    description: 'Date of birth (YYYY-MM-DD) for age calculation. Age <5 = CHILD (first child FREE), Age ≥5 = ADULT (full fare for both legs)'
  }) 
  @IsDateString() dateOfBirth: string;
  
  @ApiProperty({ 
    example: 'NATIONAL_ID', 
    enum: IdDocumentType,
    description: 'NATIONAL_ID for Ethiopians (Verifayda verified), PASSPORT for others'
  }) 
  @IsEnum(IdDocumentType) idDocumentType: IdDocumentType;
  
  @ApiPropertyOptional({ 
    example: 'ET123456789', 
    description: 'Ethiopian national ID - verified via Verifayda 2.0 (NOT stored in database)' 
  }) 
  @IsOptional() @IsString() idDocumentNumber?: string;
  
  @ApiPropertyOptional({ 
    example: 'P1234567', 
    description: 'Passport number for non-Ethiopian passengers (no verification)' 
  }) 
  @IsOptional() @IsString() passportNumber?: string;
  
  @ApiPropertyOptional({ 
    example: 'Djibouti', 
    description: 'Passport issuing country for non-Ethiopians' 
  }) 
  @IsOptional() @IsString() passportCountry?: string;
  
  @ApiPropertyOptional({ 
    example: 'Ethiopian', 
    description: 'Ethiopian (Verifayda + Telebirr/CBE/eBirr), Djiboutian (Passport + Waafi), Other (Passport + Card)' 
  }) 
  @IsOptional() @IsString() nationality?: string;
}

export class CreateBookingDto {
  @ApiProperty({ description: 'Passenger ID' }) 
  @IsString() passengerId: string;
  
  @ApiProperty({ description: 'Outbound / leg-1 schedule ID' }) 
  @IsString() scheduleId: string;
  
  @ApiProperty({ description: 'Outbound / leg-1 seat hold ID' }) 
  @IsString() holdId: string;
  
  @ApiProperty({ example: 'station-uuid', description: 'Outbound origin station UUID' }) 
  @IsString() originStationId: string;
  
  @ApiProperty({ example: 'station-uuid', description: 'Outbound destination station UUID' }) 
  @IsString() destinationStationId: string;
  
  @ApiProperty({ example: 'seat-class-uuid', description: 'Outbound seat class UUID (Economy Regular, Economy Bed, VIP Bed)' })
  @IsString() seatClassId: string;

  @ApiProperty({ 
    example: 'ONE_WAY', 
    enum: ['ONE_WAY', 'ROUND_TRIP', 'TRANSIT', 'ROUND_TRIP_TRANSIT'],
    description: `Booking type:

**ONE_WAY:** Single direct journey — needs: scheduleId, holdId. Passenger: seatId.

**ROUND_TRIP:** Outbound + return, single PNR — needs above + returnScheduleId, returnHoldId, returnOriginStationId, returnDestinationStationId. Passenger: seatId + returnSeatId.

**TRANSIT:** Single journey via connecting train, single PNR — needs above + leg2ScheduleId, leg2HoldId, transitStationId, leg2DestinationStationId. Passenger: seatId + leg2SeatId.

**ROUND_TRIP_TRANSIT:** Round trip via connecting trains — needs all 4 hold sets + all station fields. Passenger: seatId + leg2SeatId + returnSeatId + returnLeg2SeatId.`,
    default: 'ONE_WAY'
  }) 
  @IsOptional() @IsString() bookingType?: string;
  
  @ApiProperty({ 
    type: [PassengerInputDto], 
    description: `Passenger array — required seat fields vary by bookingType:

**ONE_WAY:** { seatId, passengerName, dateOfBirth, idDocumentType, … }

**ROUND_TRIP:** { seatId (outbound leg-1), returnSeatId (return leg-1), passengerName, … }

**TRANSIT:** { seatId (leg-1), leg2SeatId (leg-2), passengerName, … }

**ROUND_TRIP_TRANSIT:** { seatId, leg2SeatId, returnSeatId, returnLeg2SeatId, passengerName, … }

**Age-based pricing:** First child (<5 years) travels FREE, subsequent children pay full fare.`
  }) 
  @IsArray() @ValidateNested({ each: true }) @Type(() => PassengerInputDto) 
  passengers: PassengerInputDto[];

  @ApiPropertyOptional({ description: 'Promo code for discount (applies to combined fare for round-trip)' }) 
  @IsOptional() @IsString() promoCode?: string;
  
  @ApiPropertyOptional({ description: 'Loyalty points to redeem (applies to combined fare for round-trip)' }) 
  @IsOptional() @IsInt() loyaltyRedemptionPoints?: number;
  
  @ApiPropertyOptional({ example: 'DJF', enum: Currency, description: 'Display currency for fare breakdown (ETB, DJF, USD). Transaction always in ETB.' }) 
  @IsOptional() @IsEnum(Currency) displayCurrency?: Currency;

  // Transit-specific fields
  @ApiPropertyOptional({ description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Leg-2 schedule ID' })
  @IsOptional() @IsString() leg2ScheduleId?: string;

  @ApiPropertyOptional({ description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Leg-2 seat hold ID' })
  @IsOptional() @IsString() leg2HoldId?: string;

  @ApiPropertyOptional({ description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Transit (connecting) station UUID' })
  @IsOptional() @IsString() transitStationId?: string;

  @ApiPropertyOptional({ description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Leg-2 destination station UUID' })
  @IsOptional() @IsString() leg2DestinationStationId?: string;

  @ApiPropertyOptional({ description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Leg-2 seat class ID (defaults to outbound seatClassId)' })
  @IsOptional() @IsString() leg2SeatClassId?: string;

  // Round-trip transit: return direction transit fields
  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-1 schedule ID' })
  @IsOptional() @IsString() returnScheduleId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return origin station ID' })
  @IsOptional() @IsString() returnOriginStationId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return destination station ID' })
  @IsOptional() @IsString() returnDestinationStationId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return seat hold ID' })
  @IsOptional() @IsString() returnHoldId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return seat class ID' })
  @IsOptional() @IsString() returnSeatClassId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-2 schedule ID' })
  @IsOptional() @IsString() returnLeg2ScheduleId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-2 seat hold ID' })
  @IsOptional() @IsString() returnLeg2HoldId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return transit (connecting) station UUID' })
  @IsOptional() @IsString() returnTransitStationId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-2 destination station UUID' })
  @IsOptional() @IsString() returnLeg2DestinationStationId?: string;

  @ApiPropertyOptional({ description: '**ROUND_TRIP_TRANSIT:** Return leg-2 seat class ID' })
  @IsOptional() @IsString() returnLeg2SeatClassId?: string;
}

export class ModifyBookingDto {
  @ApiProperty() @IsString() bookingRef: string;
  @ApiProperty({ example: 'schedule-uuid' }) @IsString() newScheduleId: string;
  @ApiProperty({ type: [String] }) @IsArray() newSeatIds: string[];
  @ApiPropertyOptional() @IsOptional() @IsString() reason?: string;
}

export class CancelBookingDto {
  @ApiProperty() @IsString() bookingRef: string;
  @ApiPropertyOptional() @IsOptional() @IsString() reason?: string;
}
