import type { VehicleRecord } from '../types';
import { displayUILabel, isEmpty } from './vehicle';

/** 车辆类型标准枚举（与型号参数台账一致） */
export const VEHICLE_TYPE_OPTIONS = [
  '轻型厢式货车',
  '重型厢式货车',
  '重型半挂牵引车',
  '重型平板半挂车',
  '重型集装箱半挂车',
  '小型普通客车',
  '叉车',
  '油车',
] as const;

export type VehicleTypeOption = (typeof VEHICLE_TYPE_OPTIONS)[number];

export interface ModelMaintenanceRow {
  id: string;
  item: string;
  kmCycle: string;
  monthCycle: string;
  labor: string;
  material: string;
  total: string;
}

export interface VehicleModelParams {
  brand: string;
  model: string;
  vehicleModel: string;
  announcementModel: string;
  fuelType: string;
  vehicleType: string;
  lengthMm: string;
  widthMm: string;
  heightMm: string;
  tireCount: string;
  tireWearFee: string;
  batteryType: string;
  batteryVendor: string;
  totalPower: string;
  batteryRangeKm: string;
  hydrogenCapacity: string;
  gaugeMode: string;
  hydrogenRangeKm: string;
  coldUnitVendor: string;
  maintenanceItems: ModelMaintenanceRow[];
}

const DEFAULT_MAINTENANCE: ModelMaintenanceRow[] = [
  { id: '1', item: '变速器油', kmCycle: '60000', monthCycle: '24', labor: '0', material: '571', total: '571' },
  { id: '2', item: '变速器油', kmCycle: '60000', monthCycle: '24', labor: '0', material: '571', total: '571' },
  { id: '3', item: '变速器油', kmCycle: '60000', monthCycle: '24', labor: '0', material: '571', total: '571' },
  { id: '4', item: '变速器油', kmCycle: '60000', monthCycle: '24', labor: '0', material: '571', total: '571' },
  { id: '5', item: '变速器油', kmCycle: '60000', monthCycle: '24', labor: '0', material: '571', total: '571' },
];

function resolveVehicleTypeEnum(raw: string): string {
  if (isEmpty(raw)) return '无';
  const text = String(raw).trim();
  const exact = VEHICLE_TYPE_OPTIONS.find((item) => item === text);
  if (exact) return exact;
  const fuzzy = VEHICLE_TYPE_OPTIONS.find((item) => text.includes(item) || item.includes(text));
  if (fuzzy) return fuzzy;
  if (text.includes('牵引')) return '重型半挂牵引车';
  if (text.includes('集装箱') && text.includes('挂')) return '重型集装箱半挂车';
  if (text.includes('平板') && text.includes('挂')) return '重型平板半挂车';
  if (text.includes('客车')) return '小型普通客车';
  if (text.includes('叉车')) return '叉车';
  if (text.includes('油车') || text.includes('挂靠油')) return '油车';
  if (text.includes('轻型')) return '轻型厢式货车';
  if (text.includes('重型') && text.includes('厢')) return '重型厢式货车';
  return text;
}

function inferVehicleBodyModel(model: string): string {
  if (isEmpty(model)) return '无';
  const text = String(model).trim();
  const tonMatch = text.match(/\d+(?:\.\d+)?吨[\u4e00-\u9fa5A-Za-z/]*/);
  if (tonMatch) return tonMatch[0];
  return text;
}

function inferFuelType(brand: string, model: string): string {
  const text = `${brand}${model}`;
  if (/氢|FCEV|fuel/i.test(text)) return '氢';
  if (/电|EV/i.test(text)) return '电';
  if (/油车|挂靠油|柴油|燃油/.test(text)) return '柴油';
  return '氢';
}

function inferDimensions(vehicleType: string): { length: string; width: string; height: string } {
  const type = resolveVehicleTypeEnum(vehicleType);
  if (type === '重型半挂牵引车' || type === '轻型厢式货车') {
    return { length: '5995', width: '2145', height: '3130' };
  }
  if (type === '小型普通客车') {
    return { length: '4990', width: '1850', height: '1700' };
  }
  return { length: '11995', width: '2550', height: '3980' };
}

function inferTireCount(vehicleType: string): string {
  const type = resolveVehicleTypeEnum(vehicleType);
  if (type === '重型半挂牵引车') return '10';
  if (type === '重型集装箱半挂车' || type === '重型平板半挂车') return '8';
  return '6';
}

/** 根据车辆台账推导型号参数展示数据（只读查看，原型演示） */
export function resolveVehicleModelParams(record: VehicleRecord): VehicleModelParams {
  const brand = displayUILabel(record.brand);
  const model = displayUILabel(record.model);
  const vehicleType = resolveVehicleTypeEnum(record.vehicleType);
  const dims = inferDimensions(record.vehicleType);
  const isHydrogen = inferFuelType(record.brand, record.model) === '氢';

  return {
    brand,
    model,
    vehicleModel: inferVehicleBodyModel(record.model),
    announcementModel: isEmpty(record.model) ? '无' : record.model,
    fuelType: inferFuelType(record.brand, record.model),
    vehicleType,
    lengthMm: dims.length,
    widthMm: dims.width,
    heightMm: dims.height,
    tireCount: inferTireCount(record.vehicleType),
    tireWearFee: '0.8',
    batteryType: '磷酸铁锂',
    batteryVendor: '宁德时代新能源科技股份有限公司',
    totalPower: isHydrogen ? '100' : '282',
    batteryRangeKm: isHydrogen ? '200' : '350',
    hydrogenCapacity: isHydrogen ? '140' : '无',
    gaugeMode: isHydrogen ? 'MPa' : '无',
    hydrogenRangeKm: isHydrogen ? '1000' : '无',
    coldUnitVendor: '开利空调冷冻设备（上海）有限公司',
    maintenanceItems: DEFAULT_MAINTENANCE,
  };
}
