import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { BookingEntity } from './booking.entity';
import { CurrencyEntity } from './currency.entity';

/**
 * One row per generated customer tax invoice. Frozen snapshot of
 * everything that was billed at generation time — `guest_*_snapshot`,
 * `line_items` JSON, and `tax_bifurcation` JSON all detach the
 * invoice from the live booking so a later itinerary edit doesn't
 * mutate history.
 *
 * Voucher numbering reuses settlement_receipt_sequences with the
 * 'INV' prefix.
 */
@Entity('customer_invoices')
@Index(['tenant_id', 'invoice_year', 'invoice_no'], { unique: true })
@Index(['tenant_id', 'booking_id', 'invoice_date'])
@Index(['tenant_id', 'invoice_date'])
export class CustomerInvoiceEntity extends BaseEntity {
  @Column({ type: 'varchar', length: 20 })
  invoice_no: string;

  @Column({ type: 'int' })
  invoice_year: number;

  @ManyToOne(() => BookingEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking: BookingEntity;

  @Column({ type: 'uuid' })
  booking_id: string;

  @Column({ type: 'date' })
  invoice_date: string;

  @ManyToOne(() => CurrencyEntity)
  @JoinColumn({ name: 'currency_id' })
  currency: CurrencyEntity;

  @Column({ type: 'uuid' })
  currency_id: string;

  @Column({ type: 'varchar', length: 200, nullable: true })
  guest_name_snapshot: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  guest_mobile_snapshot: string | null;

  @Column({ type: 'varchar', length: 200, nullable: true })
  guest_email_snapshot: string | null;

  @Column({ type: 'varchar', length: 200, nullable: true })
  destination_name_snapshot: string | null;

  @Column({ type: 'date', nullable: true })
  travel_start_snapshot: string | null;

  @Column({ type: 'date', nullable: true })
  travel_end_snapshot: string | null;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  subtotal: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  tax_total: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  grand_total: number;

  /** Aggregated per-tax-component breakdown:
   *  [{ tax_type_id, tax_type_name, rate, amount }] */
  @Column({ type: 'jsonb', nullable: true })
  tax_bifurcation: Array<{
    tax_type_id: string;
    tax_type_name: string;
    rate: number;
    amount: number;
  }> | null;

  /** Frozen line snapshot:
   *  [{ kind, description, qty, unit_price, line_total }] */
  @Column({ type: 'jsonb', nullable: true })
  line_items: Array<{
    kind: 'HOTEL' | 'TRANSPORT' | 'ACTIVITY' | 'SPECIAL' | 'SERVICE';
    description: string;
    qty: number;
    unit_price: number;
    line_total: number;
  }> | null;

  @Column({ type: 'text', nullable: true })
  notes: string | null;

  @Column({ type: 'boolean', default: false })
  is_void: boolean;

  @Column({ type: 'timestamptz', nullable: true })
  voided_at: Date | null;

  @Column({ type: 'uuid', nullable: true })
  voided_by: string | null;

  @Column({ type: 'text', nullable: true })
  void_reason: string | null;
}
