import { Entity, Column, Unique, 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')
@Unique(['tenant_id', 'query_id', 'quote_number'])
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;

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

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

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

  // --- 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[];
}
