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

export enum DriverStatus {
  AVAILABLE = 'available',
  ON_TRIP = 'on_trip',
  INACTIVE = 'inactive',
}

/**
 * Driver entity — tenant-scoped.
 *
 * Drivers belong to a Supplier and operate in a Destination.
 * License number is unique per tenant.
 *
 * Form layout:
 * 1. Assignment — Destination, Supplier (filtered by destination)
 * 2. Basic Details — Name, Phone, Email, Alt Phone, License No, License Expiry
 * 3. Photo + Address
 * 4. Operations — Operational Hours, Status, Notes
 */
@Entity('drivers')
@Unique(['tenant_id', 'license_no'])
export class DriverEntity extends BaseEntity {
  // --- Destination FK ---
  @ManyToOne(() => DestinationEntity)
  @JoinColumn({ name: 'destination_id' })
  destination: DestinationEntity;

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

  // --- Supplier FK ---
  @ManyToOne(() => SupplierEntity)
  @JoinColumn({ name: 'supplier_id' })
  supplier: SupplierEntity;

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

  // --- Basic Details ---
  @Column({ type: 'varchar', length: 255 })
  name: 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: 50 })
  license_no: string;

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

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

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

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

  @Column({ type: 'enum', enum: DriverStatus, default: DriverStatus.AVAILABLE })
  status: DriverStatus;

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

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