import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryQuoteEntity } from './query-quote.entity';
import { TransportServiceEntity } from './transport-service.entity';
import { VehicleTypeEntity } from './vehicle-type.entity';
import { QueryQuoteActivityEntity } from './query-quote-activity.entity';

@Entity('query_quote_transports')
export class QueryQuoteTransportEntity extends BaseEntity {
  @ManyToOne(() => QueryQuoteEntity, (q) => q.transport_items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'quote_id' })
  quote: QueryQuoteEntity;

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

  @ManyToOne(() => TransportServiceEntity)
  @JoinColumn({ name: 'transport_service_id' })
  transport_service: TransportServiceEntity;

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

  /**
   * Vehicle type — FK into vehicle_types master. Nullable for SIC
   * lines (SIC pricing is per-person on the shared coach, no vehicle
   * axis). Display side resolves to a name via the vehicle-types
   * lookup; was a denormalised text column pre-refactor.
   */
  @ManyToOne(() => VehicleTypeEntity, { nullable: true })
  @JoinColumn({ name: 'vehicle_type_id' })
  vehicle_type: VehicleTypeEntity | null;

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

  @Column({ type: 'int', default: 1 })
  quantity: number;

  /** Pax travelling on this row — NULL = the full party (query adults /
   *  children). Capped at the query totals; for SIC the row is priced
   *  per seat, so quantity = adults + children. */
  @Column({ type: 'int', nullable: true })
  adults: number | null;

  @Column({ type: 'int', nullable: true })
  children: number | null;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  unit_price: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  subtotal: number;

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

  /**
   * Pickup time for the transport leg, 24-hour "HH:MM". Lets the
   * client see departure timing on the quote PDF and pre-fills the
   * booking_transport_items.pickup_time when the quote is converted
   * to a booking. Null when ops hasn't decided yet.
   */
  @Column({ type: 'varchar', length: 10, nullable: true })
  pickup_time: string | null;

  /**
   * Drop-off time at the destination, 24-hour "HH:MM". Same role as
   * pickup_time on the booking side — paired with it so the quote
   * shows both ends of the trip. Null when not yet set.
   */
  @Column({ type: 'varchar', length: 10, nullable: true })
  dropoff_time: string | null;

  @Column({ type: 'int', default: 0 })
  sort_order: number;

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

  /**
   * Per-row pickup + drop-off addresses — mirrors booking_transport_items.
   * Auto-populated from the Transport Master's from/to_address (or city
   * fallback) when the row is created via the picker; ops can override
   * inline. Carried across on convert-to-booking so the operational
   * record starts with the same values the salesperson agreed on.
   * Movement Dashboard reads these via the booking-side mirror.
   */
  @Column({ type: 'varchar', length: 500, nullable: true })
  pickup_address: string | null;

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

  @Column({ type: 'boolean', default: false })
  is_shared: boolean;

  /**
   * Soft link to a same-day activity ("dedicated transport for this
   * activity"). When set, the FE renders this transport visually
   * grouped with the activity and co-moves them on drag / Move-to-day.
   * SET NULL on delete keeps the transport row alive when the parent
   * activity is removed — it just becomes a regular standalone leg.
   */
  @ManyToOne(() => QueryQuoteActivityEntity, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'linked_activity_id' })
  linked_activity: QueryQuoteActivityEntity | null;

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

  // ── Tax snapshot (added 2026-05-07 — see migration 1777700000000) ────
  // See query-quote-hotel.entity.ts for the policy contract.
  @Column({ type: 'jsonb', nullable: true })
  tax_breakdown: Record<string, Record<string, number>> | null;

  @Column({ type: 'decimal', precision: 12, scale: 2, nullable: true })
  tax_amount: number | null;

  /** Per-line slab snapshot (added in migration 1781100000000) — see
   *  query-quote-hotel.entity.ts for the policy. */
  @Column({ type: 'jsonb', nullable: true })
  tax_snapshot: Record<string, unknown> | null;
}
