import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Rate } from '../../rule-engine/entities/rate.entity';
import { Booking } from './booking.entity';
import { BookingRateSnapshot } from './booking-rate-snapshot.entity';

@Entity({ schema: 'freight', name: 'booking_cargo_modifier' })
@Index(['bookingId'])
@Index(['rateId'])
export class BookingCargoModifier extends BaseEntity {
  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  @ManyToOne(() => Booking, (b) => b.cargoModifiers, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking;

  /**
   * The trigger-based rate (hazard, reefer, overweight …) that produced this
   * surcharge line. Replaces the former surcharge_type link now that rates are
   * self-describing.
   */
  @Column({ name: 'rate_id', type: 'uuid' })
  rateId!: string;

  @ManyToOne(() => Rate)
  @JoinColumn({ name: 'rate_id' })
  rate?: Rate;

  @Column({ name: 'trigger_value', type: 'numeric', precision: 14, scale: 4, nullable: true })
  triggerValue?: number | null;

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

  @Column({ name: 'rate_snapshot_id', type: 'uuid' })
  rateSnapshotId!: string;

  @ManyToOne(() => BookingRateSnapshot)
  @JoinColumn({ name: 'rate_snapshot_id' })
  rateSnapshot?: BookingRateSnapshot;
}
