<?php

namespace Modules\Vrc\Services\Adapters\Stub;

use Modules\Vrc\Services\Contracts\RepairInfoServiceContract;

/**
 * Offline RMI stub — returns structured, plausible reference data so the RMI UI
 * and use-case flow are fully exercisable without a paid provider subscription.
 * Replace the binding in Config/config.php with a real provider adapter for
 * production ISO 18541 access.
 */
class StubRepairInfoService implements RepairInfoServiceContract
{
    public function provider(): string
    {
        return 'stub';
    }

    public function decodeVin(string $vin): array
    {
        $vin = strtoupper(trim($vin));
        // WMI (first 3 chars) → a rough region/maker hint for the demo.
        $wmi = substr($vin, 0, 3);
        $yearChar = strlen($vin) >= 10 ? $vin[9] : '';
        return [
            'status'    => strlen($vin) >= 11 ? 'ok' : 'not_found',
            'vin'       => $vin,
            'wmi'       => $wmi,
            'region'    => $this->region($wmi[0] ?? ''),
            'model_year'=> $this->yearFromChar($yearChar),
            'note'      => 'Decoded by offline stub — connect a licensed VIN/RMI provider for authoritative data.',
        ];
    }

    public function lookup(string $useCase, array $params): array
    {
        $label = trim(($params['make'] ?? '') . ' ' . ($params['model'] ?? '') . ' ' . ($params['year'] ?? ''));
        $catalogue = [
            'maintenance' => ['Scheduled maintenance', [
                'Engine oil & filter — every 10,000 km / 12 months',
                'Air filter — every 20,000 km',
                'Brake fluid — every 2 years',
                'Coolant — every 40,000 km / 4 years',
            ]],
            'repair' => ['Repair procedure', [
                'Torque spec: wheel nuts 110 Nm',
                'Front brake pad replacement — service time ≈ 0.8 h',
                'Use OEM-equivalent friction material',
            ]],
            'diagnostics' => ['Diagnostic reference', [
                'P0300 — random/multiple cylinder misfire',
                'Check ignition coils, plugs, fuel trims',
            ]],
            'wiring' => ['Wiring diagram', ['Reference: body harness B12 — see provider portal for the full schematic.']],
            'tsb' => ['Technical service bulletins', ['No open TSBs in the offline dataset.']],
            'recall' => ['Recall check', ['No recalls in the offline dataset — verify with the OEM.']],
        ];
        [$title, $sections] = $catalogue[$useCase] ?? ['Reference', ['No data for this use-case in the offline dataset.']];

        return [
            'status'   => 'ok',
            'title'    => trim($title . ($label ? " — {$label}" : '')),
            'use_case' => $useCase,
            'sections' => $sections,
            'note'     => 'Offline stub data — not a substitute for licensed manufacturer RMI.',
        ];
    }

    private function region(string $c): string
    {
        return match (true) {
            in_array($c, ['1', '4', '5']) => 'North America',
            in_array($c, ['J']) => 'Japan',
            in_array($c, ['K']) => 'Korea',
            in_array($c, ['S', 'V', 'W', 'Z']) => 'Europe',
            default => 'Unknown',
        };
    }

    private function yearFromChar(string $c): ?int
    {
        $map = ['A'=>2010,'B'=>2011,'C'=>2012,'D'=>2013,'E'=>2014,'F'=>2015,'G'=>2016,'H'=>2017,'J'=>2018,'K'=>2019,'L'=>2020,'M'=>2021,'N'=>2022,'P'=>2023,'R'=>2024,'S'=>2025,'T'=>2026];
        return $map[strtoupper($c)] ?? null;
    }
}
