import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { BookingEntity } from './booking.entity';
import { TransportServiceEntity } from './transport-service.entity';
import { SupplierEntity } from './supplier.entity';
import { DriverEntity } from './driver.entity';
import { VehicleEntity } from './vehicle.entity';

@Entity('booking_transport_items')
export class BookingTransportItemEntity extends BaseEntity {
  @ManyToOne(() => BookingEntity, (b) => b.transport_items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking: BookingEntity;

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

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

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

  @Column({ type: 'varchar', length: 100 })
  vehicle_type: string;

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

  @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;

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

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

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

  // --- Driver Assignment (Phase B — nullable for now) ---
  @ManyToOne(() => SupplierEntity, { nullable: true })
  @JoinColumn({ name: 'supplier_id' })
  supplier: SupplierEntity | null;

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

  @ManyToOne(() => DriverEntity, { nullable: true })
  @JoinColumn({ name: 'driver_id' })
  driver: DriverEntity | null;

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

  /**
   * Free-text fallbacks for when the actual supplier / driver / vehicle
   * isn't a registered master — real-case: private taxi booked ad-hoc,
   * supplier dispatches a driver not yet in our system, etc.
   * If the corresponding *_id is set, the text field is ignored at read time.
   */
  @Column({ type: 'varchar', length: 255, nullable: true })
  supplier_name_text: string | null;

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

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

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

  @ManyToOne(() => VehicleEntity, { nullable: true })
  @JoinColumn({ name: 'vehicle_id' })
  vehicle: VehicleEntity | null;

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

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

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

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

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