import { Entity, Column, ManyToOne, JoinColumn, Index } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { BookingSpecialItemEntity } from './booking-special-item.entity';
import { SupplierEntity } from './supplier.entity';
import { DriverEntity } from './driver.entity';

/**
 * One row per (special_item, supplier) assignment. Mirrors the
 * transport + activity junction tables so ad-hoc specials also get
 * multi-supplier support once the FE is cut over in Phase 2.
 */
@Entity('booking_special_item_suppliers')
@Index(['tenant_id', 'special_item_id'])
@Index(['tenant_id', 'supplier_id'])
export class BookingSpecialItemSupplierEntity extends BaseEntity {
  @ManyToOne(() => BookingSpecialItemEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'special_item_id' })
  special_item: BookingSpecialItemEntity;

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

  @ManyToOne(() => SupplierEntity, { nullable: true })
  @JoinColumn({ name: 'supplier_id' })
  supplier: SupplierEntity | null;

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

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

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

  @Column({ type: 'varchar', length: 30, default: 'primary' })
  role: string;

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

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

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

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

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

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