import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { CurrencyEntity } from './currency.entity';

/**
 * Destination entity — tenant-scoped.
 *
 * Sections:
 * - Basic Details: Name, Short Code, Country, City, Currency (FK), Timezone, Description, Image
 * - Visa Information: Visa Required (boolean)
 * - Services: Available service types (hotels, transport, activities)
 */
@Entity('destinations')
@Index(['tenant_id', 'short_code'], { unique: true, where: '"is_deleted" = false' })
export class DestinationEntity extends BaseEntity {
  // --- Basic Details ---
  @Column({ type: 'varchar', length: 255 })
  name: string;

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

  @Column({ type: 'varchar', length: 100 })
  country: string;

  @Column({ type: 'varchar', length: 100, default: '' })
  city: string;

  @ManyToOne(() => CurrencyEntity)
  @JoinColumn({ name: 'currency_id' })
  currency: CurrencyEntity;

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

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

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

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

  // --- Visa Information ---
  @Column({ type: 'boolean', default: false })
  visa_required: boolean;

  // --- Tax Types applicable at this destination ---
  @Column({ type: 'jsonb', nullable: true })
  tax_types: string[] | null;

  // --- Services available at this destination ---
  @Column({ type: 'simple-json', nullable: true })
  services: string[] | null;

  // --- Exchange rate (USD → base currency) ---
  // `currency_id` (above) already points to this destination's base
  // currency master (AED for Dubai, EUR for Paris, …). These two
  // columns carry the live conversion factor + its last-edit stamp so
  // the app can project USD-equivalent amounts without hitting an FX
  // API on every read, and can flag staleness when ops hasn't
  // refreshed the rate in a while.
  //
  // Convention: `usd_to_base_rate` = how many base-currency units per
  // 1 USD. For Dubai → AED: ~3.672. Stored as decimal string by
  // TypeORM to preserve precision across reads.
  @Column({ type: 'decimal', precision: 12, scale: 6, nullable: true })
  usd_to_base_rate: string | null;

  @Column({ type: 'timestamp', nullable: true })
  rate_updated_at: Date | null;

  /**
   * Default tax slab for this destination (added in migration
   * 1781100000000). Powers the bifurcation hierarchy:
   *   hotel / special → destinations.tax_slab_id
   *   transport       → transport_services.tax_slab_id ?? this
   *   activity        → activities.tax_slab_id        ?? this
   * Nullable — destinations without a slab resolve at tax = 0.
   */
  @Column({ type: 'uuid', nullable: true })
  tax_slab_id: string | null;
}
