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

export const RATE_TYPES = [
  'CONTAINER_IMPORT',
  'CONTAINER_EXPORT',
  'BULK_IMPORT',
  'BULK_EXPORT',
  'INTERCITY_BULK',
  'INTERCITY_CONTAINER',
  'FIRST_MILE',
  'LAST_MILE',
  'DEMURRAGE',
  'LASHING',
  'DOUBLE_HANDLING',
  'CONTAINER_WITH_RETURN',
  'CANCELLATION_FEE',
  'OVERWEIGHT_PER_TON',
  'HAZARD_SURCHARGE',
  'REEFER_SURCHARGE',
  'PIL_EXTRA_FEE',
] as const;

export type RateType = typeof RATE_TYPES[number];

export const RATE_STATUSES = ['DRAFT', 'PENDING_APPROVAL', 'LIVE', 'SUPERSEDED'] as const;
export type RateStatus = typeof RATE_STATUSES[number];

export const RATE_UNITS = ['PER_WAGON', 'PER_TON', 'PER_CONTAINER', 'PER_KM', 'FLAT'] as const;
export type RateUnit = typeof RATE_UNITS[number];

@Entity({ schema: 'freight', name: 'rates' })
@Index(['rateType'])
@Index(['status'])
@Index(['effectiveFrom'])
@Index(['containerTypeId'])
export class Rate extends BaseEntity {
  @Column({ name: 'rate_type', type: 'varchar', length: 50 })
  rateType!: RateType;

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

  @ManyToOne(() => ContainerType, { nullable: true, eager: false })
  @JoinColumn({ name: 'container_type_id' })
  containerType?: ContainerType | null;

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

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

  @Column({ name: 'rate_value', type: 'numeric', precision: 14, scale: 4 })
  rateValue!: number;

  @Column({ name: 'rate_unit', type: 'varchar', length: 30 })
  rateUnit!: RateUnit;

  @Column({ name: 'status', type: 'varchar', length: 20, default: 'DRAFT' })
  status!: RateStatus;

  @Column({ name: 'proposed_by_staff_id', type: 'uuid' })
  proposedByStaffId!: string;

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

  @Column({ name: 'approved_at', type: 'timestamptz', nullable: true })
  approvedAt?: Date | null;

  @Column({ name: 'effective_from', type: 'date' })
  effectiveFrom!: Date;

  @Column({ name: 'effective_to', type: 'date', nullable: true })
  effectiveTo?: Date | null;
}
