<?php

namespace App\Data;

/**
 * Amadast courier IDs for shipping-calculator `couriers[]`
 * and matching against API item `id` / `shipping_method`.
 *
 * Aligned with official WordPress plugin AMDSP_ShippingMethod (v3.3.0)
 * plus Deca Post (id=795) confirmed via live calculator API.
 */
class AmadastCouriers
{
    public const PISHTAZ         = 13;
    public const TIPAX           = 4;
    public const MAHEX           = 16;
    public const CHAPAR          = 18;
    public const FORWARD_EXPRESS = 19;
    public const FORWARD_PISHTAZ = 22;
    public const PEYK            = 25;
    public const BOXIT           = 26;
    public const DIGI_EXPRESS    = 793;
    public const PARSI_POST      = 794;
    public const DEKA_POST       = 795;

    /**
     * @return array<int, string>
     */
    public static function labels(): array
    {
        return [
            self::PISHTAZ         => 'پست پیشتاز',
            self::TIPAX           => 'تیپاکس',
            self::MAHEX           => 'ماهکس',
            self::CHAPAR          => 'چاپار',
            self::FORWARD_EXPRESS => 'فوروارد اکسپرس',
            self::FORWARD_PISHTAZ => 'فوروارد پیشتاز',
            self::PEYK            => 'پیک محلی',
            self::BOXIT           => 'باکسیت',
            self::DIGI_EXPRESS    => 'دیجی اکسپرس',
            self::PARSI_POST      => 'پارسی پست',
            self::DEKA_POST       => 'دکا پست',
        ];
    }

    /**
     * @return array<int, array{label: string, icon: string, note: string}>
     */
    public static function adminOptions(): array
    {
        return [
            self::PISHTAZ => [
                'label' => 'پست پیشتاز',
                'icon'  => 'ti-mail',
                'note'  => '',
            ],
            self::TIPAX => [
                'label' => 'تیپاکس',
                'icon'  => 'ti-truck',
                'note'  => '',
            ],
            self::MAHEX => [
                'label' => 'ماهکس',
                'icon'  => 'ti-package',
                'note'  => '',
            ],
            self::CHAPAR => [
                'label' => 'چاپار',
                'icon'  => 'ti-bike',
                'note'  => '',
            ],
            self::FORWARD_EXPRESS => [
                'label' => 'فوروارد اکسپرس',
                'icon'  => 'ti-plane',
                'note'  => 'فقط تهران↔مشهد',
            ],
            self::FORWARD_PISHTAZ => [
                'label' => 'فوروارد پیشتاز',
                'icon'  => 'ti-mail-forward',
                'note'  => '',
            ],
            self::PEYK => [
                'label' => 'پیک محلی',
                'icon'  => 'ti-motorbike',
                'note'  => 'مبدأ=مقصد',
            ],
            self::BOXIT => [
                'label' => 'باکسیت',
                'icon'  => 'ti-box',
                'note'  => '',
            ],
            self::DIGI_EXPRESS => [
                'label' => 'دیجی اکسپرس',
                'icon'  => 'ti-building-store',
                'note'  => '',
            ],
            self::PARSI_POST => [
                'label' => 'پارسی پست',
                'icon'  => 'ti-mail-opened',
                'note'  => '',
            ],
            self::DEKA_POST => [
                'label' => 'دکا پست',
                'icon'  => 'ti-truck-delivery',
                'note'  => '',
            ],
        ];
    }

    /**
     * @return list<int>
     */
    public static function ids(): array
    {
        return array_map('intval', array_keys(self::labels()));
    }

    public static function isValid(int $id): bool
    {
        return isset(self::labels()[$id]);
    }

    /**
     * Match calculator API item against a configured courier id.
     * API returns both legacy `id` (e.g. 793) and `shipping_method` (e.g. 30).
     */
    public static function itemMatches(array $svc, int $courierId): bool
    {
        return (int) ($svc['id'] ?? 0) === $courierId
            || (int) ($svc['shipping_method'] ?? 0) === $courierId;
    }
}
