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

export class GuestPassengerDto {
  @ApiProperty({ example: 'seat-uuid', description: 'Outbound / leg-1 seat ID (all booking types)' }) 
  @IsString() seatId: string;

  @ApiPropertyOptional({ example: 'return-seat-uuid', description: '**ROUND_TRIP / ROUND_TRIP_TRANSIT:** Return leg-1 seat ID. Required for ROUND_TRIP and ROUND_TRIP_TRANSIT.' })
  @IsOptional() @IsString() returnSeatId?: string;

  @ApiPropertyOptional({ example: 'leg2-seat-uuid', description: '**TRANSIT / ROUND_TRIP_TRANSIT:** Outbound leg-2 seat ID. Required for TRANSIT and ROUND_TRIP_TRANSIT.' })
  @IsOptional() @IsString() leg2SeatId?: string;

  @ApiPropertyOptional({ example: 'ret-leg2-seat-uuid', description: '**ROUND_TRIP_TRANSIT:** Return leg-2 seat ID. Required for ROUND_TRIP_TRANSIT.' })
  @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' }) 
  @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)' }) 
  @IsOptional() @IsString() idDocumentNumber?: string;

  @ApiPropertyOptional({ example: 'P1234567', description: 'Passport number for non-Ethiopians' }) 
  @IsOptional() @IsString() passportNumber?: string;

  @ApiPropertyOptional({ example: 'Djibouti', description: 'Passport issuing country' }) 
  @IsOptional() @IsString() passportCountry?: string;

  @ApiPropertyOptional({ example: 'Ethiopian', description: 'Ethiopian (Verifayda), Djiboutian, Other' }) 
  @IsOptional() @IsString() nationality?: string;

  @ApiPropertyOptional({ example: '+251912345678', description: 'Contact phone number' }) 
  @IsOptional() @IsString() phone?: string;

  @ApiPropertyOptional({ example: 'abebe@email.com', description: 'Contact email' }) 
  @IsOptional() @IsString() email?: string;
}

export class CreateGuestBookingDto {
  @ApiPropertyOptional({ 
    example: 'ONE_WAY', 
    enum: ['ONE_WAY', 'ROUND_TRIP', 'TRANSIT', 'ROUND_TRIP_TRANSIT'], 
    default: 'ONE_WAY',
    description: `Booking type:
**ONE_WAY:** scheduleId + holdId. Passenger: seatId.
**ROUND_TRIP:** above + returnScheduleId/returnHoldId/returnOriginStationId/returnDestinationStationId. Passenger: seatId + returnSeatId.
**TRANSIT:** above + leg2ScheduleId/leg2HoldId/transitStationId/leg2DestinationStationId. Passenger: seatId + leg2SeatId.
**ROUND_TRIP_TRANSIT:** all 4 hold sets + all station fields. Passenger: seatId + leg2SeatId + returnSeatId + returnLeg2SeatId.`
  })
  @IsOptional() @IsString() bookingType?: 'ONE_WAY' | 'ROUND_TRIP' | 'TRANSIT' | 'ROUND_TRIP_TRANSIT';

  @ApiProperty({ example: 'schedule-uuid', description: 'Outbound / leg-1 schedule UUID' }) 
  @IsString() scheduleId: string;

  @ApiProperty({ example: 'hold-uuid', description: 'Outbound seat hold UUID' }) 
  @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' })
  @IsString() seatClassId: string;

  @ApiPropertyOptional({ example: 'seat-class-uuid', description: 'ROUND_TRIP / ROUND_TRIP_TRANSIT: return seat class UUID' })
  @IsOptional() @IsString() returnSeatClassId?: string;

  @ApiPropertyOptional({ example: 'schedule-uuid', description: 'TRANSIT / ROUND_TRIP_TRANSIT: leg-2 schedule UUID' })
  @IsOptional() @IsString() leg2ScheduleId?: string;

  @ApiPropertyOptional({ example: 'hold-uuid', description: 'TRANSIT / ROUND_TRIP_TRANSIT: leg-2 seat hold UUID' })
  @IsOptional() @IsString() leg2HoldId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'TRANSIT / ROUND_TRIP_TRANSIT: connecting station UUID' })
  @IsOptional() @IsString() transitStationId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'TRANSIT / ROUND_TRIP_TRANSIT: leg-2 destination station UUID' })
  @IsOptional() @IsString() leg2DestinationStationId?: string;

  @ApiPropertyOptional({ example: 'seat-class-uuid', description: 'TRANSIT / ROUND_TRIP_TRANSIT: leg-2 seat class UUID' })
  @IsOptional() @IsString() leg2SeatClassId?: string;

  @ApiPropertyOptional({ example: 'schedule-uuid', description: 'ROUND_TRIP / ROUND_TRIP_TRANSIT: return leg-1 schedule UUID' })
  @IsOptional() @IsString() returnScheduleId?: string;

  @ApiPropertyOptional({ example: 'hold-uuid', description: 'ROUND_TRIP / ROUND_TRIP_TRANSIT: return seat hold UUID' })
  @IsOptional() @IsString() returnHoldId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'ROUND_TRIP / ROUND_TRIP_TRANSIT: return origin station UUID' })
  @IsOptional() @IsString() returnOriginStationId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'ROUND_TRIP / ROUND_TRIP_TRANSIT: return destination station UUID' })
  @IsOptional() @IsString() returnDestinationStationId?: string;

  @ApiPropertyOptional({ example: 'schedule-uuid', description: 'ROUND_TRIP_TRANSIT: return leg-2 schedule UUID' })
  @IsOptional() @IsString() returnLeg2ScheduleId?: string;

  @ApiPropertyOptional({ example: 'hold-uuid', description: 'ROUND_TRIP_TRANSIT: return leg-2 seat hold UUID' })
  @IsOptional() @IsString() returnLeg2HoldId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'ROUND_TRIP_TRANSIT: return transit station UUID' })
  @IsOptional() @IsString() returnTransitStationId?: string;

  @ApiPropertyOptional({ example: 'station-uuid', description: 'ROUND_TRIP_TRANSIT: return leg-2 destination station UUID' })
  @IsOptional() @IsString() returnLeg2DestinationStationId?: string;

  @ApiPropertyOptional({ example: 'seat-class-uuid', description: 'ROUND_TRIP_TRANSIT: return leg-2 seat class UUID' })
  @IsOptional() @IsString() returnLeg2SeatClassId?: string;

  @ApiProperty({ 
    type: [GuestPassengerDto], 
    description: `Passenger array. Required seat fields vary by bookingType:
- ONE_WAY: seatId
- ROUND_TRIP: seatId + returnSeatId
- TRANSIT: seatId + leg2SeatId
- ROUND_TRIP_TRANSIT: seatId + leg2SeatId + returnSeatId + returnLeg2SeatId` 
  }) 
  @IsArray() @ValidateNested({ each: true }) @Type(() => GuestPassengerDto) passengers: GuestPassengerDto[];

  @ApiPropertyOptional({ example: 'WEEKEND15' }) 
  @IsOptional() @IsString() promoCode?: string;

  @ApiPropertyOptional({ example: 'ETB', enum: Currency, description: 'Display currency (ETB, DJF, USD)' }) 
  @IsOptional() @IsEnum(Currency) displayCurrency?: Currency;

  @ApiPropertyOptional({ example: true, description: 'Create account using first passenger details' }) 
  @IsOptional() @IsBoolean() createAccount?: boolean;

  @ApiPropertyOptional({ example: 'password123', description: 'Password if createAccount is true' }) 
  @IsOptional() @IsString() password?: string;

  @ApiPropertyOptional({ example: true, description: 'Save passenger details for future bookings' }) 
  @IsOptional() @IsBoolean() savePassengerDetails?: boolean;

  @ApiPropertyOptional({ example: 'device-uuid-12345', description: 'Device ID for local storage of passenger details' }) 
  @IsOptional() @IsString() deviceId?: string;
}

export class SavedPassengerProfileDto {
  @ApiProperty() passengerName: string;
  @ApiProperty() dateOfBirth: string;
  @ApiProperty({ enum: IdDocumentType }) idDocumentType: IdDocumentType;
  @ApiPropertyOptional() idDocumentNumber?: string;
  @ApiPropertyOptional() passportNumber?: string;
  @ApiPropertyOptional() passportCountry?: string;
  @ApiPropertyOptional() nationality?: string;
  @ApiPropertyOptional() phone?: string;
  @ApiPropertyOptional() email?: string;
}

export class GetSavedPassengersDto {
  @ApiPropertyOptional({ example: 'device-uuid-12345', description: 'Device ID to retrieve saved passengers' }) 
  @IsOptional() @IsString() deviceId?: string;
}
