<?php

namespace App\Ocr;

/**
 * WMI (World Manufacturer Identifier) → marque véhicule.
 * Les 3 premiers caractères du VIN identifient le constructeur.
 * Port fidèle de lib/vin-make.ts.
 */
final class VinMakeResolver
{
    private const WMI_TABLE = [
        // France
        'VF1' => 'Renault', 'VF2' => 'Renault', 'VF3' => 'Peugeot', 'VF6' => 'Renault Trucks',
        'VF7' => 'Citroën', 'VF8' => 'DS Automobiles', 'VFA' => 'Renault', 'VFB' => 'Renault Trucks',
        'VFC' => 'DS Automobiles', 'UU1' => 'Dacia',
        // Allemagne
        'WVW' => 'Volkswagen', 'WV1' => 'Volkswagen', 'WV2' => 'Volkswagen', 'WV3' => 'Volkswagen',
        'WBA' => 'BMW', 'WBS' => 'BMW M', 'WBY' => 'BMW i', 'WBX' => 'BMW',
        'WDB' => 'Mercedes-Benz', 'WDC' => 'Mercedes-Benz', 'WDD' => 'Mercedes-Benz', 'WME' => 'Smart',
        'WAU' => 'Audi', 'WA1' => 'Audi', 'WP0' => 'Porsche', 'WP1' => 'Porsche',
        'W0L' => 'Opel', 'TRU' => 'Audi',
        // Royaume-Uni
        'SAL' => 'Land Rover', 'SAJ' => 'Jaguar', 'SCF' => 'Aston Martin', 'SFD' => 'Ford',
        'SB1' => 'Toyota', 'MAT' => 'Vauxhall', 'SAR' => 'Rover',
        // République tchèque / Slovaquie
        'TMB' => 'Škoda', 'TMA' => 'Opel',
        // Espagne
        'VSS' => 'SEAT', 'VS6' => 'SEAT', 'VS7' => 'SEAT', 'VAN' => 'Nissan', 'NM0' => 'Ford',
        // Italie
        'ZAR' => 'Alfa Romeo', 'ZFA' => 'Fiat', 'ZFF' => 'Ferrari', 'ZLA' => 'Lancia',
        'ZAM' => 'Maserati', 'ZHW' => 'Lamborghini',
        // Suède
        'YV1' => 'Volvo', 'YV4' => 'Volvo',
        // Corée du Sud
        'KMH' => 'Hyundai', 'KMF' => 'Hyundai', 'KNA' => 'Kia', 'KNB' => 'Kia', 'U5Y' => 'Kia',
        // Japon
        'JHM' => 'Honda', 'JTD' => 'Toyota', 'JTM' => 'Toyota', 'JN1' => 'Nissan', 'JN6' => 'Nissan',
        'JAA' => 'Honda', 'JA3' => 'Mitsubishi', 'JA4' => 'Mitsubishi',
        // États-Unis
        '1G1' => 'Chevrolet', '1FA' => 'Ford', '1FB' => 'Ford', '1FC' => 'Ford', '1FT' => 'Ford',
        '1HG' => 'Honda', '2HG' => 'Honda', '3VW' => 'Volkswagen',
    ];

    public function getVehicleMake(?string $vin): string
    {
        if (null === $vin || strlen($vin) < 3) {
            return '';
        }

        $wmi = strtoupper(substr($vin, 0, 3));

        return self::WMI_TABLE[$wmi] ?? '';
    }
}
