import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { ActivityEntity } from './activity.entity';
import { SupplierEntity } from './supplier.entity';
import { ActivityTicketItemEntity } from './activity-ticket-item.entity';

export enum TicketBatchStatus {
  ACTIVE = 'ACTIVE',
  EXHAUSTED = 'EXHAUSTED',
  EXPIRED = 'EXPIRED',
  CANCELLED = 'CANCELLED',
}

@Entity('activity_ticket_batches')
export class ActivityTicketBatchEntity extends BaseEntity {
  @ManyToOne(() => ActivityEntity, { onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'activity_id' })
  activity: ActivityEntity;

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

  // Ticket type name (e.g. "Standard", "VIP") — free text matching activity ticket master
  @Column({ type: 'varchar', length: 100 })
  ticket_type: string;

  @Column({ type: 'varchar', length: 50 })
  age_group: string; // ADULT | CHILD | INFANT

  @ManyToOne(() => SupplierEntity, { nullable: true, onDelete: 'RESTRICT' })
  @JoinColumn({ name: 'supplier_id' })
  supplier: SupplierEntity | null;

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

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

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

  // null = unrestricted; e.g. "MORNING", "EVENING", "ALL_DAY"
  @Column({ type: 'varchar', length: 50, nullable: true })
  time_slot: string | null;

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

  // Maintained live: total_qty minus assigned (non-cancelled) items
  @Column({ type: 'int', default: 0 })
  available_qty: number;

  @Column({ type: 'enum', enum: TicketBatchStatus, default: TicketBatchStatus.ACTIVE })
  status: TicketBatchStatus;

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

  // Comma-separated booking UUIDs this batch is linked to.
  // When set, Movement Dashboard ticket assignment will only show
  // this batch to ops handling one of the listed bookings.
  @Column({ type: 'text', nullable: true })
  booking_reference_no: string | null;

  @OneToMany(() => ActivityTicketItemEntity, (item) => item.batch)
  items: ActivityTicketItemEntity[];
}
