import { BaseEntity } from "@edr/api-common";
import { Column, Entity, JoinColumn, ManyToOne } from "typeorm";

import { Invoice } from "./invoice.entity";

@Entity({ schema: "freight", name: "invoice_lines" })
export class InvoiceLine extends BaseEntity {
  @Column({ name: "invoice_id", type: "uuid", nullable: false })
  invoiceId!: string;

  @ManyToOne(() => Invoice, { onDelete: "CASCADE" })
  @JoinColumn({ name: "invoice_id" })
  invoice!: Invoice;

  @Column({ name: "charge_type", type: "varchar", nullable: false })
  chargeType!: string;

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

  /** Units this line bills for (e.g. container count, wagon count, tons). */
  @Column({ name: "quantity", type: "numeric", precision: 12, scale: 2, default: 1 })
  quantity!: number;

  /** Price per unit; `amount` is normally `quantity * unitRate`. */
  @Column({ name: "unit_rate", type: "numeric", precision: 14, scale: 2, default: 0 })
  unitRate!: number;

  @Column({
    name: "amount",
    type: "numeric",
    precision: 14,
    scale: 2,
    nullable: false,
  })
  amount!: number;

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

  @Column({ name: "metadata", type: "jsonb", nullable: true })
  metadata?: Record<string, unknown> | null;
}
