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

/**
 * What cargo sizes/commodities are in scope for a contract — NO quantities.
 * Container: one row per enabled size (20ft/40ft). Bulk: one row with a cargo
 * type. See §5.4.
 */
@Entity({ schema: 'freight', name: 'contract_cargo_scope' })
@Index(['contractId'])
export class ContractCargoScope extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

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

  /** '20ft' | '40ft'; null for bulk. */
  @Column({ name: 'container_size', type: 'varchar', length: 10, nullable: true })
  containerSize?: string | null;

  @Column({ name: 'cargo_type_id', type: 'uuid', nullable: true })
  cargoTypeId?: string | null;

  @ManyToOne(() => CargoType, { nullable: true })
  @JoinColumn({ name: 'cargo_type_id' })
  cargoType?: CargoType | null;

  @Column({ name: 'cargo_free_text', type: 'varchar', length: 200, nullable: true })
  cargoFreeText?: string | null;

  /**
   * GENERAL contracts: total cargo quantity allowed across ALL shipments on this
   * scope line over the validity window. Container → number of containers of
   * this size; bulk → tons (or items for per-item commodities). Bookings draw
   * down against it until the cap is reached. NULL = uncapped (always NULL for
   * ONE_TIME, which allows a single booking).
   */
  @Column({ name: 'quantity_cap', type: 'numeric', precision: 12, scale: 2, nullable: true })
  quantityCap?: number | null;
}
