import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index } from 'typeorm';

export const FEE_RULE_TYPES = ['STORAGE_FEE', 'DEMURRAGE_FEE'] as const;
export type FeeRuleType = (typeof FEE_RULE_TYPES)[number];

/**
 * Batch 5 — configurable storage / demurrage fee rules (no invoice/payment here — that is Batch 6).
 * The most specific active rule (highest `specificity` then lowest `priority`) applies to an item.
 * `freeDays` is the grace period; charging starts the day after it expires.
 */
@Entity({ schema: 'freight', name: 'warehouse_fee_rules' })
@Index(['ruleType'])
@Index(['isActive'])
export class WarehouseFeeRule extends BaseEntity {
  @Column({ name: 'name', type: 'varchar', length: 160 })
  name!: string;

  @Column({ name: 'rule_type', type: 'varchar', length: 20 })
  ruleType!: FeeRuleType;

  @Column({ name: 'priority', type: 'int', default: 100 })
  priority!: number;

  // ── Scope (null = applies to all) ─────────────────────────────────────────
  @Column({ name: 'freight_type', type: 'varchar', length: 16, nullable: true })
  freightType?: string | null; // CONTAINER | BULK

  @Column({ name: 'trade_direction', type: 'varchar', length: 16, nullable: true })
  tradeDirection?: string | null; // IMPORT | EXPORT | DOMESTIC | BOTH

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

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

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

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

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

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

  // ── Fee definition ────────────────────────────────────────────────────────
  @Column({ name: 'free_days', type: 'int', default: 0 })
  freeDays!: number;

  @Column({ name: 'rate_per_day', type: 'numeric', precision: 14, scale: 2, default: 0 })
  ratePerDay!: number;

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

  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive!: boolean;
}
