import { Entity, Column, Unique, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { DestinationEntity } from './destination.entity';
import { ActivityEntity } from './activity.entity';
import { ActivityTicketEntity } from './activity-ticket.entity';
import { CurrencyEntity } from './currency.entity';

export enum AgeGroup {
  CHILD = 'child',
  ADULT = 'adult',
  SENIOR = 'senior',
}

/**
 * Activity Pricing entity — tenant-scoped.
 *
 * Matrix: Activity × Ticket Type × Age Group × Currency → Default + 5 Seasonal Prices.
 * Season columns are positional (1-5 by sort_order from Season Master).
 * Auto-fetched in quotations by matching travel date to SeasonDuration ranges.
 */
@Entity('activity_pricing')
@Unique(['tenant_id', 'activity_id', 'ticket_id', 'age_group', 'currency_id'])
export class ActivityPricingEntity extends BaseEntity {
  @ManyToOne(() => DestinationEntity)
  @JoinColumn({ name: 'destination_id' })
  destination: DestinationEntity;

  @Column({ type: 'uuid' })
  destination_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: AgeGroup;

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

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

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

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

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

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

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

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

  // ── Floor pricing ────────────────────────────────────────────────
  // Lowest acceptable selling price per season for this
  // (activity × ticket × age × currency) row. Quote/Booking saves
  // validate `unit_price` against the floor for the active season;
  // below-floor is routed to a Change Request for approval.
  // NULL = no floor configured for that season, allow any price.

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

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

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

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

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

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

  // ── Tax bifurcation (Step 2 migration) ────────────────────────────
  // Each inclusive price cell is bifurcated into base + tax_amount at
  // save time so reads don't have to recompute. season_5 is intentionally
  // omitted — Phase 1 only uses 4 seasonal tiers + default.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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