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';

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

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

  @Column({ name: 'rate_id', type: 'uuid' })
  rateId!: string;

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

  @Column({ name: 'rate_type', type: 'varchar', length: 50 })
  rateType!: string;

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

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

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

  @Column({ name: 'snapshotted_at', type: 'timestamptz' })
  snapshottedAt!: Date;
}
