/**
 * Vehicle Engine Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents engine specifications for a vehicle.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Vehicle } from './vehicle.entity';
import { EngineCatalog } from './engine-catalog.entity';

export enum TurboType {
  VGT = 'VGT',
  WASTEGATE = 'wastegate',
  TWIN = 'twin',
  COMPOUND = 'compound',
}

@Entity({ name: 'vehicle_engines', schema: 'vehicle_management' })
@Index('idx_vehicle_engines_vehicle', ['vehicleId'])
@Index('idx_vehicle_engines_serial', ['serialNumber'])
@Index('idx_vehicle_engines_catalog', ['engineCatalogId'])
export class VehicleEngine {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'vehicle_id', type: 'uuid' })
  vehicleId: string;

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

  @Column({ name: 'serial_number', type: 'varchar', length: 50, nullable: true })
  serialNumber?: string;

  // Performance specs
  @Column({ type: 'integer', nullable: true })
  horsepower?: number;

  @Column({ type: 'integer', nullable: true })
  torque?: number;

  // ECM
  @Column({ name: 'ecm_model', type: 'varchar', length: 50, nullable: true })
  ecmModel?: string;

  @Column({ name: 'ecm_software', type: 'varchar', length: 50, nullable: true })
  ecmSoftware?: string;

  // Injection system
  @Column({ name: 'injection_system', type: 'varchar', length: 50, nullable: true })
  injectionSystem?: string;

  @Column({ name: 'rail_pressure_max', type: 'decimal', precision: 10, scale: 2, nullable: true })
  railPressureMax?: number;

  @Column({ name: 'injector_count', type: 'integer', nullable: true })
  injectorCount?: number;

  // Turbo
  @Column({ name: 'turbo_type', type: 'varchar', length: 50, nullable: true })
  turboType?: TurboType;

  @Column({ name: 'turbo_make', type: 'varchar', length: 50, nullable: true })
  turboMake?: string;

  @Column({ name: 'turbo_model', type: 'varchar', length: 50, nullable: true })
  turboModel?: string;

  // Dates
  @Column({ name: 'manufacture_date', type: 'date', nullable: true })
  manufactureDate?: Date;

  @Column({ name: 'rebuild_date', type: 'date', nullable: true })
  rebuildDate?: Date;

  @Column({ name: 'rebuild_odometer', type: 'integer', nullable: true })
  rebuildOdometer?: number;

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

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

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

  // Relations
  @OneToOne(() => Vehicle, vehicle => vehicle.engine, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'vehicle_id' })
  vehicle: Vehicle;

  @ManyToOne(() => EngineCatalog, { nullable: true })
  @JoinColumn({ name: 'engine_catalog_id' })
  engineCatalog?: EngineCatalog;
}
