<?php

declare(strict_types=1);

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

namespace App\Business\Settlement;

use App\Models\DberpPayableRecord;
use App\Models\DberpPaymentOrder;
use App\Models\DberpPurchaseOrder;
use App\Models\DberpReceiptOrder;
use App\Models\DberpReceivableRecord;
use App\Models\DberpSalesOrder;
use App\Models\DberpSettlementAdjustment;
use App\Models\DberpSettlementWriteOff;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

final class SettlementSourceTypeResolver
{
    /**
     * @var array<class-string<Model>, string>
     */
    private const MODEL_TO_KEY = [
        DberpSalesOrder::class => 'sales_order',
        DberpPurchaseOrder::class => 'purchase_order',
        DberpReceiptOrder::class => 'receipt_order',
        DberpPaymentOrder::class => 'payment_order',
        DberpReceivableRecord::class => 'receivable_record',
        DberpPayableRecord::class => 'payable_record',
        DberpSettlementWriteOff::class => 'settlement_write_off',
        DberpSettlementAdjustment::class => 'settlement_adjustment',
    ];

    public function fromModel(?Model $source): ?string
    {
        if ($source === null) {
            return null;
        }

        return self::MODEL_TO_KEY[$source::class] ?? Str::snake(class_basename($source));
    }

    public function normalize(?string $sourceType): ?string
    {
        if ($sourceType === null || $sourceType === '') {
            return null;
        }

        if (array_key_exists($sourceType, __('dberp.options.settlement_source_type'))) {
            return $sourceType;
        }

        if (is_subclass_of($sourceType, Model::class)) {
            return $this->fromModel(app($sourceType));
        }

        foreach (self::MODEL_TO_KEY as $class => $key) {
            if ($sourceType === $class) {
                return $key;
            }
        }

        return Str::snake(class_basename(str_replace('\\', '/', $sourceType)));
    }
}
