/**
 * FuelLog DTOs - Data Transfer Objects para Registro de Combustible
 *
 * Cargas de combustible y calculo de rendimiento.
 *
 * @module Assets (MAE-015)
 */

import {
  IsString,
  IsUUID,
  IsOptional,
  IsNumber,
  IsDateString,
  IsPositive,
  Min,
  Max,
  MaxLength,
} from 'class-validator';

/**
 * DTO para crear un nuevo registro de combustible
 */
export class CreateFuelLogDto {
  @IsUUID()
  assetId: string;

  @IsDateString()
  date: string;

  @IsOptional()
  @IsString()
  @MaxLength(8)
  time?: string;

  @IsNumber()
  @IsPositive()
  liters: number;

  @IsNumber()
  @IsPositive()
  unitPrice: number;

  @IsNumber()
  @IsPositive()
  cost: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  odometer?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  hoursReading?: number;

  @IsOptional()
  @IsString()
  @MaxLength(50)
  fuelType?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  supplier?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  invoiceNumber?: string;

  @IsOptional()
  @IsUUID()
  projectId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  location?: string;

  @IsOptional()
  @IsUUID()
  operatorId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  operatorName?: string;

  @IsOptional()
  @IsString()
  notes?: string;
}

/**
 * DTO para actualizar un registro de combustible
 */
export class UpdateFuelLogDto {
  @IsOptional()
  @IsDateString()
  date?: string;

  @IsOptional()
  @IsString()
  @MaxLength(8)
  time?: string;

  @IsOptional()
  @IsNumber()
  @IsPositive()
  liters?: number;

  @IsOptional()
  @IsNumber()
  @IsPositive()
  unitPrice?: number;

  @IsOptional()
  @IsNumber()
  @IsPositive()
  cost?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  odometer?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  hoursReading?: number;

  @IsOptional()
  @IsString()
  @MaxLength(50)
  fuelType?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  supplier?: string;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  invoiceNumber?: string;

  @IsOptional()
  @IsUUID()
  projectId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  location?: string;

  @IsOptional()
  @IsUUID()
  operatorId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  operatorName?: string;

  @IsOptional()
  @IsString()
  notes?: string;
}

/**
 * DTO para filtrar registros de combustible
 */
export class FuelLogFiltersDto {
  @IsOptional()
  @IsUUID()
  assetId?: string;

  @IsOptional()
  @IsUUID()
  projectId?: string;

  @IsOptional()
  @IsString()
  fuelType?: string;

  @IsOptional()
  @IsDateString()
  dateFrom?: string;

  @IsOptional()
  @IsDateString()
  dateTo?: string;

  @IsOptional()
  @IsNumber()
  @Min(0)
  minLiters?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  maxLiters?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  minCost?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  maxCost?: number;

  @IsOptional()
  @IsUUID()
  operatorId?: string;

  @IsOptional()
  @IsString()
  supplier?: string;

  @IsOptional()
  @IsString()
  search?: string;

  @IsOptional()
  @IsNumber()
  @Min(1)
  page?: number;

  @IsOptional()
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number;

  @IsOptional()
  @IsString()
  sortBy?: string;

  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC';
}

/**
 * DTO para reporte de consumo de combustible
 */
export class FuelConsumptionReportDto {
  @IsUUID()
  assetId: string;

  @IsDateString()
  dateFrom: string;

  @IsDateString()
  dateTo: string;
}

/**
 * DTO de respuesta para un registro de combustible
 */
export class FuelLogResponseDto {
  id: string;
  tenantId: string;
  assetId: string;
  asset?: {
    id: string;
    assetCode: string;
    name: string;
  };
  logDate: Date;
  logTime?: string;
  projectId?: string;
  location?: string;
  fuelType: string;
  quantityLiters: number;
  unitPrice: number;
  totalCost: number;
  odometerReading?: number;
  hoursReading?: number;
  kilometersSinceLast?: number;
  hoursSinceLast?: number;
  litersPer100km?: number;
  litersPerHour?: number;
  vendorName?: string;
  invoiceNumber?: string;
  operatorId?: string;
  operatorName?: string;
  notes?: string;
  createdAt: Date;
  updatedAt: Date;
}

/**
 * DTO de respuesta para resumen de consumo
 */
export class FuelConsumptionSummaryDto {
  assetId: string;
  assetCode: string;
  assetName: string;
  totalLiters: number;
  totalCost: number;
  totalKilometers?: number;
  totalHours?: number;
  averageLitersPer100km?: number;
  averageLitersPerHour?: number;
  logsCount: number;
  periodStart: Date;
  periodEnd: Date;
}
