import { Entity, Column, Index, ManyToOne, JoinColumn, Check } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { QueryQuoteHotelEntity } from './query-quote-hotel.entity';

/**
 * Quote-side twin of BookingHotelPeriodEntity. Same columns, same
 * semantics — kept as a separate table so the quote → booking
 * convert path can copy rows without cross-referencing.
 *
 * See booking-hotel-period.entity.ts for the shape rationale.
 */
@Entity('query_quote_hotel_periods')
@Index(
  'ix_query_quote_hotel_periods_tenant_item',
  ['tenant_id', 'hotel_item_id'],
  { where: '"is_deleted" = false' },
)
@Index(
  'ux_query_quote_hotel_periods_item_sort',
  ['hotel_item_id', 'sort_order'],
  { unique: true, where: '"is_deleted" = false' },
)
@Check('ck_query_quote_hotel_periods_num_rooms', '"num_rooms" >= 1')
@Check('ck_query_quote_hotel_periods_pax_per_room', '"pax_per_room" >= 1')
@Check('ck_query_quote_hotel_periods_rate_non_neg', '"rate_per_night" >= 0')
@Check(
  'ck_query_quote_hotel_periods_nights_range',
  `"nights" = '{}'::integer[] OR (0 <= ALL("nights") AND 99 >= ALL("nights"))`,
)
export class QueryQuoteHotelPeriodEntity extends BaseEntity {
  @ManyToOne(() => QueryQuoteHotelEntity, (h) => h.periods, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'hotel_item_id' })
  hotel_item: QueryQuoteHotelEntity;

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

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

  @Column({ type: 'integer', array: true, default: () => "'{}'::integer[]" })
  nights: number[];

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

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

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

  @Column({ type: 'int', default: 2 })
  pax_per_room: number;

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