import { Entity, Column, Index, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryEntity } from './query.entity';
import { CurrencyEntity } from './currency.entity';
import { QueryQuoteHotelEntity } from './query-quote-hotel.entity';
import { QueryQuoteTransportEntity } from './query-quote-transport.entity';
import { QueryQuoteActivityEntity } from './query-quote-activity.entity';
import { QueryQuoteSpecialEntity } from './query-quote-special.entity';

export enum PricingStrategy {
  OVERALL = 'overall',
  PER_COMPONENT = 'per_component',
  PER_PERSON = 'per_person',
  PER_COMPONENT_PER_PERSON = 'per_component_per_person',
}

export enum MarkupType {
  AMOUNT = 'amount',
  PERCENTAGE = 'percentage',
}

@Entity('query_quotes')
@Index(['tenant_id', 'query_id', 'quote_number'], { unique: true, where: '"is_deleted" = false' })
export class QueryQuoteEntity extends BaseEntity {
  @ManyToOne(() => QueryEntity, (q) => q.quotes, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'query_id' })
  query: QueryEntity;

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

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

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

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

  // --- Totals ---
  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  hotel_total: number;

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

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

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

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

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

  // --- Markup & Selling Price ---
  @Column({ type: 'varchar', length: 30, default: PricingStrategy.OVERALL })
  pricing_strategy: PricingStrategy;

  @ManyToOne(() => CurrencyEntity, { nullable: true })
  @JoinColumn({ name: 'selling_currency_id' })
  selling_currency: CurrencyEntity | null;

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

  @Column({ type: 'decimal', precision: 12, scale: 6, default: 1 })
  exchange_rate: number;

  @Column({ type: 'varchar', length: 10, default: MarkupType.AMOUNT })
  markup_type: MarkupType;

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

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

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

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

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

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

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

  /** FINAL customer price — cost + markup + markup_tax_amount. */
  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  selling_total: number;

  /** Tax on the markup (service charge), selling currency — derived
   *  server-side from selling_total via splitFinalPrice. */
  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  markup_tax_amount: number;

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

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

  // ── Tax config (added 2026-05-08 — see migration 1777800000000) ─────
  // Quote-level user choice that drives the Markup panel:
  //   tax_slab_id   FK-style ref to tax_slabs (current slab for this
  //                 destination); NULL = "No tax" (the None option)
  //   tax_inclusive true → markup totals already contain tax
  //                 false → tax is added on top at Final Price
  //
  // Independent of per-line `tax_breakdown` snapshots — those are
  // frozen rates for invoice / audit; these are presentation flags
  // for the markup view.
  @Column({ type: 'uuid', nullable: true })
  tax_slab_id: string | null;

  // Default flipped to `true` 2026-05-13 per client feedback —
  // all hotel / transport / activity prices are GST-inclusive by
  // default. Existing rows retain whatever flag they had (test
  // data; not back-filled). The migration that flips the column
  // default is `1778300000000-DefaultTaxInclusiveTrue`.
  @Column({ type: 'boolean', default: true })
  tax_inclusive: boolean;

  // --- Child Items ---
  @OneToMany(() => QueryQuoteHotelEntity, (i) => i.quote)
  hotel_items: QueryQuoteHotelEntity[];

  @OneToMany(() => QueryQuoteTransportEntity, (i) => i.quote)
  transport_items: QueryQuoteTransportEntity[];

  @OneToMany(() => QueryQuoteActivityEntity, (i) => i.quote)
  activity_items: QueryQuoteActivityEntity[];

  @OneToMany(() => QueryQuoteSpecialEntity, (i) => i.quote)
  special_items: QueryQuoteSpecialEntity[];
}
