<?php

declare(strict_types=1);

namespace App\Support\AuditLogDisplay;

/**
 * 将审计快照字段名解析为面向业务的中文标签；未知字段回退为可读占位，不暴露蛇形键名。
 */
final class DberpAuditLogFieldLabelResolver
{
    /**
     * @var array<string, array<string, string>>
     */
    private const SUBJECT_FIELD_OVERRIDES = [
        'DberpNumberSequence' => [
            'code' => '规则标识',
            'name' => '规则名称',
            'prefix' => '编码前缀',
            'remark' => '备注',
        ],
        'DberpUnit' => [
            'code' => '单位编码',
            'name' => '单位名称',
        ],
        'DberpProductCategory' => [
            'code' => '分类编码',
            'name' => '分类名称',
        ],
        'DberpProductBrand' => [
            'brand_code' => '品牌编码',
            'name' => '品牌名称',
            'short_name' => '品牌简称',
        ],
        'DberpProduct' => [
            'code' => '商品编码',
            'name' => '商品名称',
        ],
        'DberpCustomer' => [
            'code' => '客户编码',
            'name' => '客户名称',
        ],
        'DberpCustomerCategory' => [
            'code' => '客户分类编码',
            'name' => '客户分类名称',
        ],
        'DberpSupplier' => [
            'code' => '供应商编码',
            'name' => '供应商名称',
        ],
        'DberpSupplierCategory' => [
            'code' => '供应商分类编码',
            'name' => '供应商分类名称',
        ],
        'DberpWarehouse' => [
            'code' => '仓库编码',
            'name' => '仓库名称',
        ],
        'DberpPurchaseOrder' => [
            'code' => '采购单号',
            'document_no' => '采购单号',
        ],
        'DberpSalesOrder' => [
            'code' => '销售单号',
            'document_no' => '销售单号',
        ],
        'DberpPurchaseReceiptOrder' => [
            'code' => '到货单号',
            'document_no' => '到货单号',
        ],
        'DberpPurchaseReturnOrder' => [
            'code' => '采购退货单号',
            'document_no' => '采购退货单号',
        ],
        'DberpSalesOutboundOrder' => [
            'code' => '销售出库单号',
            'document_no' => '销售出库单号',
        ],
        'DberpSalesReturnOrder' => [
            'code' => '销售退货单号',
            'document_no' => '销售退货单号',
        ],
        'DberpTransferOrder' => [
            'code' => '调拨单号',
            'document_no' => '调拨单号',
        ],
        'DberpOtherStockInOrder' => [
            'code' => '其他入库单号',
            'document_no' => '其他入库单号',
        ],
        'DberpOtherStockOutOrder' => [
            'code' => '其他出库单号',
            'document_no' => '其他出库单号',
        ],
        'DberpStocktakeOrder' => [
            'code' => '盘点单号',
            'document_no' => '盘点单号',
        ],
        'DberpReceiptOrder' => [
            'code' => '收款单号',
            'document_no' => '收款单号',
        ],
        'DberpPaymentOrder' => [
            'code' => '付款单号',
            'document_no' => '付款单号',
        ],
        'DberpReceivableRecord' => [
            'code' => '应收单号',
            'document_no' => '应收单号',
        ],
        'DberpPayableRecord' => [
            'code' => '应付单号',
            'document_no' => '应付单号',
        ],
        'DberpSettlementStatement' => [
            'code' => '对账单号',
            'document_no' => '对账单号',
        ],
        'DberpSettlementWriteOff' => [
            'code' => '核销单号',
            'document_no' => '核销单号',
        ],
        'DberpSettlementAdjustment' => [
            'code' => '调整单号',
            'document_no' => '调整单号',
        ],
        'DberpAdminRole' => [
            'name' => '角色名称',
            'display_name' => '显示名称',
        ],
        'DberpAdminPermission' => [
            'name' => '权限标识',
            'display_name' => '权限名称',
        ],
        'User' => [
            'name' => '姓名',
            'login_account' => '登录账号',
        ],
    ];

    public function resolve(string $fieldKey, ?string $auditableType): string
    {
        $short = $this->auditableShortClass($auditableType);

        if ($short !== null && isset(self::SUBJECT_FIELD_OVERRIDES[$short][$fieldKey])) {
            return self::SUBJECT_FIELD_OVERRIDES[$short][$fieldKey];
        }

        $generic = $this->genericForeignKeyLabel($fieldKey);
        if ($generic !== null) {
            return $generic;
        }

        $fieldTranslationKey = 'dberp.fields.'.$fieldKey;
        $fieldLabel = __($fieldTranslationKey);
        if ($fieldLabel !== $fieldTranslationKey) {
            return (string) $fieldLabel;
        }

        $snapKey = 'dberp.audit.snapshot_keys.'.$fieldKey;
        $snapLabel = __($snapKey);
        if ($snapLabel !== $snapKey) {
            return (string) $snapLabel;
        }

        $auditDisplayKey = 'dberp.audit_display.generic_field_label';
        $fallback = (string) __($auditDisplayKey, ['field' => $this->humanizeUnknownField($fieldKey)]);

        return $fallback;
    }

    private function humanizeUnknownField(string $fieldKey): string
    {
        return str_replace('_', ' ', $fieldKey);
    }

    private function genericForeignKeyLabel(string $fieldKey): ?string
    {
        $map = [
            'category_id' => '商品分类',
            'customer_category_id' => '客户分类',
            'supplier_category_id' => '供应商分类',
            'warehouse_id' => '仓库',
            'product_id' => '商品',
            'unit_id' => '单位',
            'brand_id' => '品牌',
            'customer_id' => '客户',
            'supplier_id' => '供应商',
            'purchase_order_id' => '采购订单',
            'sales_order_id' => '销售订单',
            'inventory_document_id' => '库存单据',
            'region_id' => '地区',
            'handler_employee_id' => '经办职员',
            'manager_employee_id' => '负责人',
        ];

        return $map[$fieldKey] ?? null;
    }

    private function auditableShortClass(?string $auditableType): ?string
    {
        if ($auditableType === null || $auditableType === '') {
            return null;
        }

        return class_basename($auditableType);
    }
}
