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

export class CreateTravelerProfileDto {
  @ApiProperty() @IsString() passengerId: string;
  @ApiProperty({ example: 'Sara Ketsela' }) @IsString() fullName: string;
  @ApiProperty({ example: 'SPOUSE' }) @IsString() relationship: string;
  @ApiPropertyOptional({ example: '1998-04-01' }) @IsOptional() @IsDateString() dateOfBirth?: string;
  @ApiPropertyOptional({ example: 'ET-1234-5678' }) @IsOptional() @IsString() nationalId?: string;
  @ApiPropertyOptional() @IsOptional() @IsString() notes?: string;
}

export class CreateSavedRouteDto {
  @ApiProperty() @IsString() passengerId: string;
  @ApiProperty() @IsString() fromStationId: string;
  @ApiProperty() @IsString() toStationId: string;
  @ApiProperty({ example: 'Addis Ababa' }) @IsString() fromName: string;
  @ApiProperty({ example: 'Dire Dawa' }) @IsString() toName: string;
}

export class VerifyFaydaDto {
  @ApiProperty({ 
    example: 'ET123456789', 
    description: `**Ethiopian national ID number**
    
- Format: Varies by Ethiopian ID system
- Example: ET123456789
- Must be valid Ethiopian national ID
- Used to query Verifayda 2.0 government database`
  })
  @IsString()
  nationalId: string;
}

export class RegisterInternationalPassengerDto {
  @ApiProperty({ example: 'John Smith', description: 'Full name as on passport' })
  @IsString()
  passengerName: string;

  @ApiProperty({ example: '1990-07-20', description: 'Date of birth' })
  @IsDateString()
  dateOfBirth: string;

  @ApiProperty({ example: 'P1234567', description: 'Passport number' })
  @IsString()
  passportNumber: string;

  @ApiProperty({ example: 'Kenya', description: 'Passport issuing country' })
  @IsString()
  passportCountry: string;

  @ApiPropertyOptional({ example: 'Kenyan', description: 'Nationality' })
  @IsOptional()
  @IsString()
  nationality?: string;

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

  @ApiPropertyOptional({ example: 'john@example.com', description: 'Email address' })
  @IsOptional()
  @IsString()
  email?: string;

  @ApiPropertyOptional({ description: 'User ID if logged in' })
  @IsOptional()
  @IsString()
  userId?: string;

  @ApiPropertyOptional({ description: 'Device ID for guest users' })
  @IsOptional()
  @IsString()
  deviceId?: string;
}

export class SavePassengerDetailsDto {
  @ApiProperty({ example: 'Abebe Kebede', description: 'Full name of passenger' })
  @IsString()
  name: string;

  @ApiProperty({ example: '1985-03-15', description: 'Date of birth in ISO format YYYY-MM-DD' })
  @IsDateString()
  dateOfBirth: string;

  @ApiPropertyOptional({ example: 'ET123456789', description: 'Ethiopian national ID (for Ethiopian passengers)' })
  @IsOptional()
  @IsString()
  nationalId?: string;

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

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

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

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

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

  @ApiPropertyOptional({ example: 'Male', description: 'Gender' })
  @IsOptional()
  @IsString()
  gender?: string;

  @ApiPropertyOptional({ example: true, description: 'Whether this is the primary passenger' })
  @IsOptional()
  @IsBoolean()
  isPrimaryPassenger?: boolean;
}

export class SavePassengersDto {
  @ApiProperty({ type: [SavePassengerDetailsDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => SavePassengerDetailsDto)
  passengers: SavePassengerDetailsDto[];

  @ApiPropertyOptional({ description: 'User ID if logged in' })
  @IsOptional()
  @IsString()
  userId?: string;

  @ApiPropertyOptional({ description: 'Device ID for guest users' })
  @IsOptional()
  @IsString()
  deviceId?: string;
}

export class RegisterPassengerDto {
  @ApiProperty({ 
    example: 'Abebe Kebede', 
    description: 'Full name of passenger (required for all scenarios)' 
  })
  @IsString()
  passengerName: string;

  @ApiProperty({ 
    example: '1985-03-15', 
    description: 'Date of birth in ISO format YYYY-MM-DD (required for all scenarios)' 
  })
  @IsDateString()
  dateOfBirth: string;

  @ApiPropertyOptional({ 
    example: 'ET123456789', 
    description: `**Ethiopian national ID number**
    
- Triggers automatic Fayda verification if enabled
- Use for Ethiopian nationals only
- Mutually exclusive with passportNumber
- If Fayda enabled: passenger data auto-filled from government database
- If Fayda disabled: falls back to manual entry`
  })
  @IsOptional()
  @IsString()
  nationalId?: string;

  @ApiPropertyOptional({ 
    example: 'P1234567', 
    description: `**Passport number**
    
- Required for international passengers
- Mutually exclusive with nationalId
- No verification performed (manual entry only)`
  })
  @IsOptional()
  @IsString()
  passportNumber?: string;

  @ApiPropertyOptional({ 
    example: 'Kenya', 
    description: 'Passport issuing country (required if passportNumber provided)' 
  })
  @IsOptional()
  @IsString()
  passportCountry?: string;

  @ApiPropertyOptional({ 
    example: 'Ethiopian', 
    description: `**Nationality**
    
- Auto-filled if Fayda verification succeeds
- Required for international passengers
- Optional for Ethiopian passengers (defaults to "Ethiopian")`
  })
  @IsOptional()
  @IsString()
  nationality?: string;

  @ApiPropertyOptional({ 
    example: '+251911234567', 
    description: 'Phone number in international format (optional but recommended)' 
  })
  @IsOptional()
  @IsString()
  phone?: string;

  @ApiPropertyOptional({ 
    example: 'abebe@example.com', 
    description: 'Email address (optional but recommended)' 
  })
  @IsOptional()
  @IsString()
  email?: string;

  @ApiPropertyOptional({ 
    description: `**User ID (auto-populated from JWT token)**
    
- Do NOT send this field in request
- Automatically extracted from JWT token if present
- Used to link passenger to user account`
  })
  @IsOptional()
  @IsString()
  userId?: string;

  @ApiPropertyOptional({ 
    example: 'device-uuid-123', 
    description: `**Device ID for guest users**
    
- **Required if no JWT token provided (guest mode)**
- Generate once and store locally (localStorage/AsyncStorage)
- Used to retrieve saved passenger profiles
- Format: UUID or any unique string`
  })
  @IsOptional()
  @IsString()
  deviceId?: string;

  @ApiPropertyOptional({ 
    example: 'Male',
    description: 'Gender (auto-filled if Fayda verification succeeds)' 
  })
  @IsOptional()
  @IsString()
  gender?: string;

  @ApiPropertyOptional({ 
    example: true,
    description: `**Whether to verify with Fayda (auto-determined)**
    
- Default: Auto-detect (true if nationalId provided)
- Set to false to skip Fayda verification (use manual entry)
- Only applicable for Ethiopian nationals with nationalId`
  })
  @IsOptional()
  @IsBoolean()
  verifyWithFayda?: boolean;
}
