<?php

namespace App\Support;

class ShipmentStatus
{
    public const STATUSES = [
        'Order Confirmed',
        'Processing Shipment',
        'Picked by Courier',
        'Customs Inspection',
        'Customs Clearance',
        'In transit',
        'On The Way',
        'Out for delivery',
        'Awaiting Payment',
        'Custom Hold',
        'Seized by custom',
        'Delivered',
    ];

    private const ALIASES = [
        'In Transit' => 'In transit',
    ];

    private const HOLD_STATUSES = [
        'Awaiting Payment',
        'Custom Hold',
        'Customs Inspection',
    ];

    private const DANGER_STATUSES = [
        'Seized by custom',
    ];

    public static function all(): array
    {
        return self::STATUSES;
    }

    public static function normalize(?string $status): string
    {
        $status = trim((string) $status);

        return self::ALIASES[$status] ?? $status;
    }

    public static function bootstrapClass(?string $status): string
    {
        $status = self::normalize($status);

        if ($status === 'Delivered') {
            return 'success';
        }

        if (in_array($status, self::DANGER_STATUSES, true)) {
            return 'danger';
        }

        if (in_array($status, self::HOLD_STATUSES, true)) {
            return 'warning';
        }

        return 'info';
    }

    public static function tailwindBadge(?string $status): string
    {
        return match (self::bootstrapClass($status)) {
            'success' => 'bg-green-100 text-green-800',
            'danger' => 'bg-red-100 text-red-800',
            'warning' => 'bg-amber-100 text-amber-800',
            default => 'bg-blue-100 text-blue-800',
        };
    }

    public static function tailwindSolid(?string $status): string
    {
        return match (self::bootstrapClass($status)) {
            'success' => 'bg-green-500 text-white',
            'danger' => 'bg-red-500 text-white',
            'warning' => 'bg-amber-500 text-white',
            default => 'bg-primary-600 text-white',
        };
    }

    public static function adminStateClass(?string $status): string
    {
        return match (self::bootstrapClass($status)) {
            'success' => 'delivered',
            'danger', 'warning' => 'hold',
            default => 'active',
        };
    }

    public static function faIcon(?string $status): string
    {
        return match (self::normalize($status)) {
            'Order Confirmed' => 'fa-check-circle',
            'Processing Shipment' => 'fa-clipboard-list',
            'Picked by Courier' => 'fa-box',
            'Customs Inspection' => 'fa-search',
            'Customs Clearance' => 'fa-file-invoice',
            'In transit', 'On The Way' => 'fa-truck',
            'Out for delivery' => 'fa-shipping-fast',
            'Awaiting Payment' => 'fa-credit-card',
            'Custom Hold' => 'fa-hourglass-half',
            'Seized by custom' => 'fa-exclamation-triangle',
            'Delivered' => 'fa-check',
            default => 'fa-map-marker-alt',
        };
    }

    public static function emailConfig(?string $status): array
    {
        return match (self::bootstrapClass($status)) {
            'success' => ['color' => '#059669', 'icon' => '&#10003;', 'bg' => '#d1fae5'],
            'danger' => ['color' => '#dc2626', 'icon' => '&#9888;', 'bg' => '#fee2e2'],
            'warning' => ['color' => '#d97706', 'icon' => '&#9888;', 'bg' => '#fef3c7'],
            default => ['color' => '#0369a1', 'icon' => '&#9679;', 'bg' => '#e0f2fe'],
        };
    }

    public static function progressIndex(?string $status): int
    {
        $index = array_search(self::normalize($status), self::STATUSES, true);

        return $index === false ? -1 : $index;
    }
}
