import { Entity, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryQuoteEntity } from './query-quote.entity';
import { HotelEntity } from './hotel.entity';
import { QueryQuoteHotelPeriodEntity } from './query-quote-hotel-period.entity';
import { QueryQuoteHotelExtraEntity } from './query-quote-hotel-extra.entity';

/**
 * Quote-side "stay envelope". Mirrors BookingHotelItemEntity minus
 * supplier fields (those are booking-only). See that file for the
 * per-night child-table design.
 */
@Entity('query_quote_hotels')
export class QueryQuoteHotelEntity extends BaseEntity {
  @ManyToOne(() => QueryQuoteEntity, (q) => q.hotel_items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'quote_id' })
  quote: QueryQuoteEntity;

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

  @ManyToOne(() => HotelEntity)
  @JoinColumn({ name: 'hotel_id' })
  hotel: HotelEntity;

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

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

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

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

  /**
   * Per-stay free-text Remarks — early check-in, infant cot, dietary
   * notes, accessibility. Carried through to booking_hotel_items.notes
   * on convert so operational staff don't have to re-type. Mirrors
   * query_quote_transports.notes / query_quote_activities.notes.
   */
  @Column({ type: 'varchar', length: 500, nullable: true })
  notes: string | null;

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

  // ── Tax snapshot (added 2026-05-07 — see migration 1777700000000) ────
  // Frozen rate components per (tax_type → component → rate_pct).
  // Snapshotted on quote save (Q1=C); preserved on edit unless the
  // line is brand new or carries refresh_tax: true (Q2=B).
  @Column({ type: 'jsonb', nullable: true })
  tax_breakdown: Record<string, Record<string, number>> | null;

  // Cached `subtotal × Σ rates / 100`, in selling currency (Q3=A).
  // Recomputed on every save so qty edits stay correct without
  // disturbing the frozen breakdown.
  @Column({ type: 'decimal', precision: 12, scale: 2, nullable: true })
  tax_amount: number | null;

  /**
   * Per-line tax-bifurcation snapshot (added in migration 1781100000000,
   * written by the new Step 3f flow). Stores the slab structure
   * resolved via TaxSlabResolverService — { slab_id, slab_name,
   * total_rate, components: [{ name, rate }] }. Sits ALONGSIDE the
   * older tax_breakdown — that one remains the source of truth for
   * existing renderers until the bifurcation UI lands; this one
   * powers the new per-line slab chip and the totals roll-up.
   *
   * Frozen at save time so master renames/deletes don't break already-
   * written lines.
   */
  @Column({ type: 'jsonb', nullable: true })
  tax_snapshot: Record<string, unknown> | null;

  // ── Children (per-night config) ───────────────────────────────────
  @OneToMany(() => QueryQuoteHotelPeriodEntity, (p) => p.hotel_item, {
    cascade: false,
  })
  periods: QueryQuoteHotelPeriodEntity[];

  @OneToMany(() => QueryQuoteHotelExtraEntity, (e) => e.hotel_item, {
    cascade: false,
  })
  extras: QueryQuoteHotelExtraEntity[];
}
