import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { BookingEntity } from './booking.entity';
import { BookingTransportItemEntity } from './booking-transport-item.entity';

/**
 * BookingFlightDetailEntity — tenant-scoped, child of BookingEntity.
 *
 * Captures airline flight info attached to a specific airport-flagged
 * transport item on a booking (e.g. inbound airport pickup, outbound
 * airport drop). A single booking can hold multiple flight rows
 * (typical case: arrival + departure), each linked to its own
 * transport item via `transport_item_id`.
 *
 * Linked transport item must reference a transport_service whose
 * `is_airport_service = true`. The booking-edit form gates the
 * flight-detail input on that flag, but no DB constraint enforces
 * it — keeps writes flexible if ops needs to attach a flight to a
 * non-airport service for some odd case.
 */
@Entity('booking_flight_details')
export class BookingFlightDetailEntity extends BaseEntity {
  @ManyToOne(() => BookingEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking: BookingEntity;

  @Column({ type: 'uuid' })
  booking_id: string;

  /**
   * The transport item this flight is attached to. Optional — ops can
   * record a flight on the booking before transport rows are finalized
   * (e.g. quote phase) and link them later.
   */
  @ManyToOne(() => BookingTransportItemEntity, { onDelete: 'CASCADE', nullable: true })
  @JoinColumn({ name: 'transport_item_id' })
  transport_item: BookingTransportItemEntity | null;

  @Column({ type: 'uuid', nullable: true })
  transport_item_id: string | null;

  /**
   * Flight direction relative to the trip. Free-form text label —
   * "Arrival" / "Departure" / "Connection 1" — so DMCs with non-
   * standard itineraries (multi-city, layovers) aren't boxed in.
   */
  @Column({ type: 'varchar', length: 50, nullable: true })
  direction: string | null;

  @Column({ type: 'varchar', length: 50 })
  flight_number: string;

  @Column({ type: 'varchar', length: 100, nullable: true })
  airline: string | null;

  /** ICAO/IATA codes — kept short for FIDS-style read */
  @Column({ type: 'varchar', length: 10, nullable: true })
  origin_airport: string | null;

  @Column({ type: 'varchar', length: 10, nullable: true })
  destination_airport: string | null;

  /**
   * Scheduled times stored as ISO strings (YYYY-MM-DDTHH:MM) so the
   * frontend datetime-local input can round-trip without timezone
   * conversion gymnastics. Both nullable — incomplete bookings often
   * have flight number but no time yet.
   */
  @Column({ type: 'varchar', length: 25, nullable: true })
  departure_time: string | null;

  @Column({ type: 'varchar', length: 25, nullable: true })
  arrival_time: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  terminal: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  gate: string | null;

  @Column({ type: 'varchar', length: 100, nullable: true })
  pnr: string | null;

  @Column({ type: 'text', nullable: true })
  notes: string | null;
}
