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

/**
 * Transport Service entity — tenant-scoped.
 *
 * Represents a transport route between two locations within a destination.
 * Categories: PICKUP, DROPOFF, ONE_WAY, TWO_WAY
 *
 * Sections:
 * - Basic Details: Short Code, Destination, Category
 * - Route: From (city/address/GPS), To (city/address/GPS)
 * - Distance & Duration
 * - Description
 */
@Entity('transport_services')
@Index(['tenant_id', 'short_code'], { unique: true, where: '"is_deleted" = false' })
export class TransportServiceEntity 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: 50 })
  category: string; // PICKUP | DROPOFF | ONE_WAY | TWO_WAY

  // --- Route: From ---
  @Column({ type: 'varchar', length: 100 })
  from_city: string;

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

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

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

  // --- Route: To ---
  @Column({ type: 'varchar', length: 100 })
  to_city: string;

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

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

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

  // --- Return Route (Two-way only) ---
  @Column({ type: 'varchar', length: 100, nullable: true })
  return_from_city: string | null;

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

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

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

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

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

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

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

  // --- Distance & Duration ---
  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
  distance_km: number | null;

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

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

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