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

export enum VehicleStatus {
  AVAILABLE = 'available',
  ASSIGNED = 'assigned',
  MAINTENANCE = 'maintenance',
}

/**
 * Vehicle entity — tenant-scoped.
 *
 * Vehicles belong to a Supplier, operate in a Destination, and can have
 * an assigned Driver (from the same Supplier).
 *
 * Form layout:
 * 1. Assignment — Destination, Supplier, Assigned Driver (filtered by supplier)
 * 2. Vehicle Details — Vehicle No, Type, Company, Model, Year, Capacity
 * 3. Additional — Fuel Type, Color, Insurance Expiry, Features
 * 4. Status + Notes
 */
@Entity('vehicles')
@Unique(['tenant_id', 'vehicle_no'])
export class VehicleEntity extends BaseEntity {
  // --- Destination FK ---
  @ManyToOne(() => DestinationEntity, { nullable: true })
  @JoinColumn({ name: 'destination_id' })
  destination: DestinationEntity | null;

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

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

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

  // --- Assigned Driver FK (optional) ---
  @ManyToOne(() => DriverEntity, { nullable: true })
  @JoinColumn({ name: 'assigned_driver_id' })
  assigned_driver: DriverEntity | null;

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

  // --- Vehicle Details ---
  @Column({ type: 'varchar', length: 50, nullable: true })
  vehicle_no: string | null;

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

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

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

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

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

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

  // --- Additional ---
  @Column({ type: 'varchar', length: 50, nullable: true })
  fuel_type: string | null;

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

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

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

  // --- Status + Notes ---
  @Column({ type: 'enum', enum: VehicleStatus, default: VehicleStatus.AVAILABLE })
  status: VehicleStatus;

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

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