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

/**
 * Supplier entity — tenant-scoped.
 *
 * A supplier can provide transport, activity, or BOTH services.
 * supplier_types is a JSON array: ['transport'], ['activity'], or ['transport','activity'].
 *
 * Form layout:
 * 1. Supplier Type — checkboxes (transport/activity) + Destination
 * 2. Common Details — name, contact, phone, email, alt_phone, photo, address
 * 3. Transport Details — linked services, op area/hours, fleet count, notes (when transport checked)
 * 4. Activity Details — linked activities, op area/hours, capacity, pricing type, notes (when activity checked)
 */
@Entity('suppliers')
@Unique(['tenant_id', 'name', 'destination_id'])
export class SupplierEntity extends BaseEntity {
  // --- Destination FK ---
  @ManyToOne(() => DestinationEntity)
  @JoinColumn({ name: 'destination_id' })
  destination: DestinationEntity;

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

  // --- Type ---
  @Column({ type: 'simple-json' })
  supplier_types: string[];

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

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

  @Column({ type: 'varchar', length: 20 })
  phone: string;

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

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

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

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

  // --- Transport Details (per-type) ---
  @Column({ type: 'simple-json', nullable: true })
  linked_transport_services: string[] | null;

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

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

  @Column({ type: 'int', nullable: true })
  fleet_count: number | null;

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

  // --- Activity Details (per-type) ---
  @Column({ type: 'simple-json', nullable: true })
  linked_activities: string[] | null;

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

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

  @Column({ type: 'int', nullable: true })
  capacity: number | null;

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

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

  // --- Legacy columns (kept for backward compat, will be null for new records) ---
  @Column({ type: 'varchar', length: 255, nullable: true })
  operational_area: string | null;

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

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

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