import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { AgentCostEntity } from './agent-cost.entity';

/**
 * Trip Source entity — tenant-scoped.
 *
 * Source Types:
 * - B2B_AGENT: Agent/intermediary, accounting for both guest + source
 * - DIRECT: Direct leads (website, referrals, ads), accounting for guests only
 *
 * Sections:
 * - Source Type Selection
 * - Basic Details: Name, Short Name
 * - Contact Person: Name, Email, Phone (with country code)
 * - Address: City, State, Country, Pin Code, Street, Locality, Landmark
 * - Billing Details: Billing Name, Additional Details
 */
@Entity('trip_sources')
@Index(['tenant_id', 'short_name'], { unique: true, where: '"is_deleted" = false' })
@Index(['tenant_id', 'short_code'], { unique: true, where: '"is_deleted" = false' })
export class TripSourceEntity extends BaseEntity {
  // --- Source Type ---
  @Column({ type: 'varchar', length: 50 })
  source_type: string; // B2B_AGENT | DIRECT

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

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

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

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

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

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

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

  // --- Address ---
  @Column({ type: 'varchar', length: 100, nullable: true })
  city: string | null;

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

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

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

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

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

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

  // --- Billing Details ---
  @Column({ type: 'varchar', length: 255, nullable: true })
  billing_name: string | null;

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

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

  /**
   * Agent Cost rule. Any source type may point at one rule.
   */
  @Column({ type: 'uuid', nullable: true })
  agent_cost_id: string | null;

  @ManyToOne(() => AgentCostEntity, { nullable: true, eager: false })
  @JoinColumn({ name: 'agent_cost_id' })
  agent_cost: AgentCostEntity | null;
}
