<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Support;

use Filament\Support\Icons\Heroicon;

/**
 * 表格中带 url 的 TextColumn、可点击 Stat 卡片图标。
 *
 * 约定：列只需 ->url(...)，图标由 {@see DberpFilamentActionConfigurator} 按列名自动注入。
 */
final class DberpFilamentLinkColumnIconResolver
{
    /**
     * @var array<string, Heroicon>
     */
    private const EXACT = [
        'code' => Heroicon::DocumentText,
        'document_number' => Heroicon::DocumentText,
        'name' => Heroicon::UserCircle,
        'product.name' => Heroicon::Cube,
        'product_code' => Heroicon::Cube,
        'sku_code' => Heroicon::Tag,
        'customer.name' => Heroicon::UserGroup,
        'supplier.name' => Heroicon::BuildingStorefront,
        'salesOrder.code' => Heroicon::ShoppingCart,
        'purchaseOrder.code' => Heroicon::ClipboardDocumentList,
    ];

    public static function forColumnName(?string $name): Heroicon|string|null
    {
        if ($name === null || $name === '') {
            return null;
        }

        if (isset(self::EXACT[$name])) {
            return self::EXACT[$name];
        }

        if (str_contains($name, 'document_number') || str_ends_with($name, '.code') || $name === 'code') {
            return Heroicon::DocumentText;
        }

        if (str_contains($name, 'customer')) {
            return Heroicon::UserGroup;
        }

        if (str_contains($name, 'supplier')) {
            return Heroicon::BuildingStorefront;
        }

        if (str_contains($name, 'product') || str_contains($name, 'sku')) {
            return Heroicon::Cube;
        }

        if (str_contains($name, 'sales_order') || str_contains($name, 'salesOrder')) {
            return Heroicon::ShoppingCart;
        }

        if (str_contains($name, 'purchase_order') || str_contains($name, 'purchaseOrder')) {
            return Heroicon::ClipboardDocumentList;
        }

        if (str_contains($name, 'warehouse')) {
            return Heroicon::BuildingOffice2;
        }

        if (str_contains($name, 'inventory')) {
            return Heroicon::ArchiveBox;
        }

        return Heroicon::ArrowTopRightOnSquare;
    }

    public static function forStatDescription(?string $description): Heroicon|string|null
    {
        if ($description === null || $description === '') {
            return Heroicon::ArrowTopRightOnSquare;
        }

        $normalized = mb_strtolower($description);

        if (str_contains($normalized, 'receivable') || str_contains($normalized, '应收')) {
            return Heroicon::CurrencyDollar;
        }

        if (str_contains($normalized, 'payable') || str_contains($normalized, '应付')) {
            return Heroicon::CurrencyDollar;
        }

        if (str_contains($normalized, 'receipt') || str_contains($normalized, '收款')) {
            return Heroicon::Banknotes;
        }

        if (str_contains($normalized, 'payment') || str_contains($normalized, '付款')) {
            return Heroicon::Banknotes;
        }

        if (str_contains($normalized, 'purchase') || str_contains($normalized, '采购')) {
            return Heroicon::ClipboardDocumentList;
        }

        if (str_contains($normalized, 'sales') || str_contains($normalized, '销售')) {
            return Heroicon::ShoppingCart;
        }

        if (str_contains($normalized, 'inventory') || str_contains($normalized, '库存')) {
            return Heroicon::ArchiveBox;
        }

        return Heroicon::ArrowTopRightOnSquare;
    }
}
