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

/**
 * Activity entity — tenant-scoped.
 *
 * Tourist activities/excursions linked to a destination.
 *
 * Sections:
 * - Basic Details: Name, Destination, Address, Location
 * - Schedule: Time Slot, Operating Days, Duration
 * - GPS: Latitude, Longitude
 * - Contact: Contact Person, Phone
 * - Additional: Special Instructions, Meeting Point, Description
 */
@Entity('activities')
@Unique(['tenant_id', 'name', 'destination_id'])
@Unique(['tenant_id', 'short_code'])
export class ActivityEntity extends BaseEntity {
  // --- Destination FK ---
  @ManyToOne(() => DestinationEntity)
  @JoinColumn({ name: 'destination_id' })
  destination: DestinationEntity;

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

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

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

  @Column({ type: 'varchar', length: 255 })
  name: string;

  @Column({ type: 'varchar', length: 500 })
  address: string;

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

  // --- Schedule & Cut-off ---
  @Column({ type: 'varchar', length: 20, nullable: true })
  cutoff_type: string | null; // 'hours_before' | 'fixed_time'

  @Column({ type: 'varchar', length: 20, nullable: true })
  cutoff_value: string | null; // e.g. "24" (hours) or "18:00" (fixed time)

  @Column({ type: 'jsonb', nullable: true })
  operating_days: string[] | null;

  @Column({ type: 'jsonb', nullable: true })
  maintenance_periods: { start: string; end: string }[] | null;

  // --- GPS ---
  @Column({ type: 'varchar', length: 20, nullable: true })
  gps_latitude: string | null;

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

  // --- Contact ---
  @Column({ type: 'varchar', length: 255 })
  contact_person: string;

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

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

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

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

  // --- Age Configuration ---
  @Column({ type: 'simple-json', nullable: true })
  age_config: { label: string; min_age: number; max_age: number }[] | null;

  // --- Tickets (child records) ---
  @OneToMany(() => ActivityTicketEntity, (ticket) => ticket.activity)
  tickets: ActivityTicketEntity[];

  // --- Media ---
  @Column({ type: 'varchar', length: 500, nullable: true })
  cover_image: string | null;

  @Column({ type: 'jsonb', nullable: true })
  gallery: { url: string; type: string; filename: string }[] | null;

  // --- Status ---
  @Column({ type: 'boolean', default: true })
  is_active: boolean;
}
