import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Yard } from '../../rule-engine/entities/yard.entity';
import { Contract } from './contract.entity';

/**
 * An allowed origin→destination lane of a contract. Routes carry NO quantity —
 * the contract scope just lists which lanes shipments may use. See §5.3.
 */
@Entity({ schema: 'freight', name: 'contract_routes' })
@Index(['contractId'])
export class ContractRoute extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

  @ManyToOne(() => Contract, (c) => c.routes, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'contract_id' })
  contract?: Contract;

  @Column({ name: 'origin_yard_id', type: 'uuid' })
  originYardId!: string;

  @ManyToOne(() => Yard)
  @JoinColumn({ name: 'origin_yard_id' })
  originYard?: Yard;

  @Column({ name: 'destination_yard_id', type: 'uuid' })
  destinationYardId!: string;

  @ManyToOne(() => Yard)
  @JoinColumn({ name: 'destination_yard_id' })
  destinationYard?: Yard;

  /** Road billing distance; null for rail-only. */
  @Column({ name: 'km', type: 'numeric', precision: 10, scale: 2, nullable: true })
  km?: number | null;

  @Column({ name: 'sort_order', type: 'smallint', default: 0 })
  sortOrder!: number;
}
