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

/**
 * Frozen UNIT rates at contract submit time — one row per rate line. The booking
 * computes totals from these × entered quantities. See §5.7.
 */
@Entity({ schema: 'freight', name: 'contract_rate_snapshots' })
@Index(['contractId'])
export class ContractRateSnapshot extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

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

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

  @Column({ name: 'rate_code', type: 'varchar', length: 64 })
  rateCode!: string;

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

  @Column({ name: 'unit_price', type: 'numeric', precision: 14, scale: 2 })
  unitPrice!: number;

  /** per_container | per_ton | per_item | per_km | flat */
  @Column({ name: 'unit_of_measure', type: 'varchar', length: 32 })
  unitOfMeasure!: string;

  @Column({ name: 'currency', type: 'varchar', length: 5 })
  currency!: string;

  @Column({ name: 'container_size', type: 'varchar', length: 10, nullable: true })
  containerSize?: string | null;

  @Column({ name: 'is_surcharge', type: 'boolean', default: false })
  isSurcharge!: boolean;

  /** is_hazardous | is_reefer when this is a conditional surcharge. */
  @Column({ name: 'conditional_on', type: 'varchar', length: 32, nullable: true })
  conditionalOn?: string | null;

  /**
   * Customs clearance service fee line — billed on the booking invoice
   * together with the freight (no separate prepaid clearance invoice).
   */
  @Column({ name: 'is_clearance', type: 'boolean', default: false })
  isClearance!: boolean;
}
