import { Entity, Column, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryQuoteEntity } from './query-quote.entity';
import { ActivityEntity } from './activity.entity';
import { ActivityTicketEntity } from './activity-ticket.entity';

@Entity('query_quote_activities')
export class QueryQuoteActivityEntity extends BaseEntity {
  @ManyToOne(() => QueryQuoteEntity, (q) => q.activity_items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'quote_id' })
  quote: QueryQuoteEntity;

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

  @ManyToOne(() => ActivityEntity)
  @JoinColumn({ name: 'activity_id' })
  activity: ActivityEntity;

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

  @ManyToOne(() => ActivityTicketEntity, { nullable: true })
  @JoinColumn({ name: 'ticket_id' })
  ticket: ActivityTicketEntity | null;

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

  @Column({ type: 'varchar', length: 50 })
  age_group: string;

  @Column({ type: 'int', default: 1 })
  quantity: number;

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

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

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

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

  @Column({ type: 'int', default: 0 })
  sort_order: number;

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

  // ── Tax snapshot (added 2026-05-07 — see migration 1777700000000) ────
  // See query-quote-hotel.entity.ts for the policy contract.
  @Column({ type: 'jsonb', nullable: true })
  tax_breakdown: Record<string, Record<string, number>> | null;

  @Column({ type: 'decimal', precision: 12, scale: 2, nullable: true })
  tax_amount: number | null;

  /** Per-line slab snapshot (added in migration 1781100000000) — see
   *  query-quote-hotel.entity.ts for the policy. */
  @Column({ type: 'jsonb', nullable: true })
  tax_snapshot: Record<string, unknown> | null;
}
