/**
 * Vehicle Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents vehicles registered in the workshop.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Fleet } from './fleet.entity';
import { VehicleEngine } from './vehicle-engine.entity';

export enum VehicleType {
  TRUCK = 'truck',
  TRAILER = 'trailer',
  BUS = 'bus',
  PICKUP = 'pickup',
  OTHER = 'other',
}

export enum VehicleStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  SOLD = 'sold',
}

@Entity({ name: 'vehicles', schema: 'vehicle_management' })
@Index('idx_vehicles_tenant', ['tenantId'])
@Index('idx_vehicles_customer', ['customerId'])
@Index('idx_vehicles_fleet', ['fleetId'])
@Index('idx_vehicles_vin', ['vin'])
@Index('idx_vehicles_plate', ['licensePlate'])
export class Vehicle {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'customer_id', type: 'uuid' })
  customerId: string;

  @Column({ name: 'fleet_id', type: 'uuid', nullable: true })
  fleetId?: string;

  // Identification
  @Column({ type: 'varchar', length: 17, nullable: true })
  vin?: string;

  @Column({ name: 'license_plate', type: 'varchar', length: 15 })
  licensePlate: string;

  @Column({ name: 'economic_number', type: 'varchar', length: 20, nullable: true })
  economicNumber?: string;

  // Vehicle info
  @Column({ type: 'varchar', length: 50 })
  make: string;

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

  @Column({ type: 'integer' })
  year: number;

  @Column({ type: 'varchar', length: 30, nullable: true })
  color?: string;

  @Column({
    name: 'vehicle_type',
    type: 'varchar',
    length: 30,
    default: VehicleType.TRUCK,
  })
  vehicleType: VehicleType;

  // Odometer
  @Column({ name: 'current_odometer', type: 'integer', nullable: true })
  currentOdometer?: number;

  @Column({ name: 'odometer_updated_at', type: 'timestamptz', nullable: true })
  odometerUpdatedAt?: Date;

  @Column({ name: 'photo_url', type: 'varchar', length: 500, nullable: true })
  photoUrl?: string;

  @Column({
    type: 'varchar',
    length: 20,
    default: VehicleStatus.ACTIVE,
  })
  status: VehicleStatus;

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

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  // Relations
  @ManyToOne(() => Fleet, fleet => fleet.vehicles, { nullable: true })
  @JoinColumn({ name: 'fleet_id' })
  fleet?: Fleet;

  @OneToOne(() => VehicleEngine, engine => engine.vehicle)
  engine?: VehicleEngine;

  // @OneToMany(() => ServiceOrder, order => order.vehicle)
  // serviceOrders: ServiceOrder[];

  // @OneToMany(() => MaintenanceReminder, reminder => reminder.vehicle)
  // reminders: MaintenanceReminder[];

  // @OneToMany(() => VehicleDocument, doc => doc.vehicle)
  // documents: VehicleDocument[];
}
