/**
 * FuelLog Entity - Registro de Combustible
 *
 * Cargas de combustible y calculo de rendimiento.
 *
 * @module Assets (MAE-015)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Asset } from './asset.entity';

@Entity('fuel_logs', { schema: 'assets' })
@Index(['tenantId', 'assetId'])
@Index(['tenantId', 'logDate'])
export class FuelLog {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Activo
  @Column({ name: 'asset_id', type: 'uuid' })
  assetId!: string;

  @ManyToOne(() => Asset, (asset) => asset.fuelLogs)
  @JoinColumn({ name: 'asset_id' })
  asset!: Asset;

  // Fecha y ubicacion
  @Column({ name: 'log_date', type: 'date' })
  logDate!: Date;

  @Column({ name: 'log_time', type: 'time', nullable: true })
  logTime?: string;

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

  @Column({ length: 255, nullable: true })
  location?: string;

  // Combustible
  @Column({ name: 'fuel_type', length: 50 })
  fuelType!: string;

  @Column({ name: 'quantity_liters', type: 'decimal', precision: 10, scale: 2 })
  quantityLiters!: number;

  @Column({ name: 'unit_price', type: 'decimal', precision: 18, scale: 4 })
  unitPrice!: number;

  @Column({ name: 'total_cost', type: 'decimal', precision: 18, scale: 2 })
  totalCost!: number;

  // Metricas al cargar
  @Column({ name: 'odometer_reading', type: 'decimal', precision: 12, scale: 2, nullable: true })
  odometerReading?: number;

  @Column({ name: 'hours_reading', type: 'decimal', precision: 12, scale: 2, nullable: true })
  hoursReading?: number;

  // Rendimiento calculado
  @Column({ name: 'kilometers_since_last', type: 'decimal', precision: 12, scale: 2, nullable: true })
  kilometersSinceLast?: number;

  @Column({ name: 'hours_since_last', type: 'decimal', precision: 12, scale: 2, nullable: true })
  hoursSinceLast?: number;

  @Column({ name: 'liters_per_100km', type: 'decimal', precision: 8, scale: 2, nullable: true })
  litersPer100km?: number;

  @Column({ name: 'liters_per_hour', type: 'decimal', precision: 8, scale: 2, nullable: true })
  litersPerHour?: number;

  // Proveedor
  @Column({ name: 'vendor_name', length: 255, nullable: true })
  vendorName?: string;

  @Column({ name: 'invoice_number', length: 100, nullable: true })
  invoiceNumber?: string;

  // Operador
  @Column({ name: 'operator_id', type: 'uuid', nullable: true })
  operatorId?: string;

  @Column({ name: 'operator_name', length: 255, nullable: true })
  operatorName?: string;

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

  // Auditoria
  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

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

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