<?php

namespace Core\App\Services\Shipping;

class EcotrackStatusMapper
{
    public static function normalize(?string $status): string
    {
        $s = trim((string)$status);
        $s = mb_strtolower($s);
        return $s;
    }

    public static function meta(?string $status): array
    {
        $s = self::normalize($status);

        // Default
        $meta = [
            'code'      => $s ?: 'unknown',
            'label_key' => 'ecotrack.status.unknown',
            'fallback'  => 'Unknown',
            'badge'     => 'bg-secondary',
            'group'     => 'unknown', // in_progress | delivered | issues | finance | unknown
        ];

        // OFFICIAL ECOTRACK STATUSES (from /get/orders/status)
        $map = [
            'prete_a_expedier' => ['ecotrack.status.prete_a_expedier', 'Ready to ship', 'bg-secondary', 'in_progress'],
            'en_ramassage' => ['ecotrack.status.en_ramassage', 'Pickup in progress', 'bg-info', 'in_progress'],
            'en_preparation_stock' => ['ecotrack.status.en_preparation_stock', 'Stock preparation', 'bg-info', 'in_progress'],
            'vers_hub' => ['ecotrack.status.vers_hub', 'To hub', 'bg-primary', 'in_progress'],
            'en_hub' => ['ecotrack.status.en_hub', 'At hub', 'bg-primary', 'in_progress'],
            'vers_wilaya' => ['ecotrack.status.vers_wilaya', 'To wilaya', 'bg-primary', 'in_progress'],
            'en_preparation' => ['ecotrack.status.en_preparation', 'In preparation', 'bg-info', 'in_progress'],
            'en_livraison' => ['ecotrack.status.en_livraison', 'Out for delivery', 'bg-info', 'in_progress'],
            'suspendu' => ['ecotrack.status.suspendu', 'Suspended', 'bg-warning text-dark', 'issues'],

            // Delivered / Finance-like states
            'livre_non_encaisse' => ['ecotrack.status.livre_non_encaisse', 'Delivered (not collected)', 'bg-success', 'delivered'],
            'encaisse_non_paye' => ['ecotrack.status.encaisse_non_paye', 'Collected (unpaid)', 'bg-success', 'finance'],
            'paiements_prets' => ['ecotrack.status.paiements_prets', 'Payments ready', 'bg-success', 'finance'],
            'paye_et_archive' => ['ecotrack.status.paye_et_archive', 'Paid & archived', 'bg-success', 'finance'],

            // Returns
            'retour_chez_livreur' => ['ecotrack.status.retour_chez_livreur', 'Return at courier', 'bg-warning text-dark', 'issues'],
            'retour_transit_entrepot' => ['ecotrack.status.retour_transit_entrepot', 'Return in warehouse transit', 'bg-warning text-dark', 'issues'],
            'retour_en_traitement' => ['ecotrack.status.retour_en_traitement', 'Return processing', 'bg-warning text-dark', 'issues'],
            'retour_recu' => ['ecotrack.status.retour_recu', 'Return received', 'bg-warning text-dark', 'issues'],
            'retour_archive' => ['ecotrack.status.retour_archive', 'Return archived', 'bg-warning text-dark', 'issues'],

            // Cancel
            'annule' => ['ecotrack.status.annule', 'Cancelled', 'bg-danger', 'issues'],
        ];

        if (isset($map[$s])) {
            [$k, $fallback, $badge, $group] = $map[$s];
            $meta['label_key'] = $k;
            $meta['fallback']  = $fallback;
            $meta['badge']     = $badge;
            $meta['group']     = $group;
        }

        return $meta;
    }

    public static function timelineSteps(): array
    {
        // Ordered progress (official statuses only)
        return [
            ['code' => 'prete_a_expedier', 'label_key' => 'ecotrack.step.prete_a_expedier', 'fallback' => 'Ready to ship', 'icon' => 'bi-file-earmark-text'],
            ['code' => 'en_ramassage', 'label_key' => 'ecotrack.step.en_ramassage', 'fallback' => 'Pickup in progress', 'icon' => 'bi-box-seam'],
            ['code' => 'en_preparation_stock', 'label_key' => 'ecotrack.step.en_preparation_stock', 'fallback' => 'Stock preparation', 'icon' => 'bi-boxes'],
            ['code' => 'vers_hub', 'label_key' => 'ecotrack.step.vers_hub', 'fallback' => 'To hub', 'icon' => 'bi-truck'],
            ['code' => 'en_hub', 'label_key' => 'ecotrack.step.en_hub', 'fallback' => 'At hub', 'icon' => 'bi-building'],
            ['code' => 'vers_wilaya', 'label_key' => 'ecotrack.step.vers_wilaya', 'fallback' => 'To wilaya', 'icon' => 'bi-truck-flatbed'],
            ['code' => 'en_preparation', 'label_key' => 'ecotrack.step.en_preparation', 'fallback' => 'In preparation', 'icon' => 'bi-gear'],
            ['code' => 'en_livraison', 'label_key' => 'ecotrack.step.en_livraison', 'fallback' => 'Out for delivery', 'icon' => 'bi-truck'],
            ['code' => 'livre_non_encaisse', 'label_key' => 'ecotrack.step.livre_non_encaisse', 'fallback' => 'Delivered', 'icon' => 'bi-check-circle'],
        ];
    }

    public static function activeIndex(?string $status): int
    {
        $s = self::normalize($status);
        $steps = self::timelineSteps();

        $idx = -1;
        foreach ($steps as $i => $step) {
            if ($s === $step['code']) {
                $idx = $i;
                break;
            }
        }

        // Finance statuses = after delivered
        if (in_array($s, ['encaisse_non_paye', 'paiements_prets', 'paye_et_archive'], true)) {
            return count($steps) - 1;
        }

        return $idx;
    }

    public static function isReturnOrCancelled(?string $status): bool
    {
        $s = self::normalize($status);
        if ($s === 'annule') return true;
        if (str_starts_with($s, 'retour_')) return true;
        return false;
    }
}
