import { Entity, Column, Unique, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryEntity } from './query.entity';
import { QueryQuoteEntity } from './query-quote.entity';
import { CurrencyEntity } from './currency.entity';
import { BookingHotelItemEntity } from './booking-hotel-item.entity';
import { BookingTransportItemEntity } from './booking-transport-item.entity';
import { BookingActivityItemEntity } from './booking-activity-item.entity';
import { BookingSpecialItemEntity } from './booking-special-item.entity';

export enum BookingStatus {
  UPCOMING = 'upcoming',
  ONGOING = 'ongoing',
  COMPLETED = 'completed',
  CANCELLED = 'cancelled',
}

/**
 * Booking entity — tenant-scoped.
 *
 * Single source of truth for operational + selling pricing of a confirmed trip.
 * Basic details (guest info, dates, pax, destination, currency, sales_team, tags)
 * live on the linked QueryEntity and are read via the `query` relation.
 * Edits to basic details are routed to QueryService.update under the hood.
 */
@Entity('bookings')
@Unique(['tenant_id', 'booking_number'])
export class BookingEntity extends BaseEntity {
  @Column({ type: 'varchar', length: 20 })
  booking_number: string;

  // --- Single source of truth for basic details ---
  @ManyToOne(() => QueryEntity)
  @JoinColumn({ name: 'query_id' })
  query: QueryEntity;

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

  // --- Selected quote snapshot ---
  @ManyToOne(() => QueryQuoteEntity)
  @JoinColumn({ name: 'quote_id' })
  quote: QueryQuoteEntity;

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

  // --- Totals (operational cost in query.currency) ---
  @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;

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

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

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

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

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

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

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

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

  // --- Booking stage (independent of query.stage) ---
  @Column({ type: 'varchar', length: 20, default: BookingStatus.UPCOMING })
  status: BookingStatus;

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

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

  @OneToMany(() => BookingTransportItemEntity, (i) => i.booking)
  transport_items: BookingTransportItemEntity[];

  @OneToMany(() => BookingActivityItemEntity, (i) => i.booking)
  activity_items: BookingActivityItemEntity[];

  @OneToMany(() => BookingSpecialItemEntity, (i) => i.booking)
  special_items: BookingSpecialItemEntity[];
}
