<?php

declare(strict_types=1);

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

namespace App\Support;

use App\Models\DberpCustomer;
use App\Models\DberpSettlementWriteOff;
use App\Models\DberpSupplier;
use Illuminate\Support\Collection;

/**
 * 核销列表 counterparty 名称批量解析，避免逐行 value() 查询。
 */
final class DberpSettlementWriteOffCounterpartyNames
{
    /**
     * @var array<int, string>
     */
    private array $customers = [];

    /**
     * @var array<int, string>
     */
    private array $suppliers = [];

    /**
     * @param  iterable<int, DberpSettlementWriteOff>|Collection<int, DberpSettlementWriteOff>  $records
     */
    public function preload(iterable $records): void
    {
        $this->customers = [];
        $this->suppliers = [];

        $collection = $records instanceof Collection ? $records : collect($records);

        if ($collection->isEmpty()) {
            return;
        }

        $customerIds = $collection
            ->where('counterparty_type', 'customer')
            ->pluck('counterparty_id')
            ->filter()
            ->unique()
            ->map(fn (mixed $id): int => (int) $id)
            ->all();

        $supplierIds = $collection
            ->where('counterparty_type', 'supplier')
            ->pluck('counterparty_id')
            ->filter()
            ->unique()
            ->map(fn (mixed $id): int => (int) $id)
            ->all();

        if ($customerIds !== []) {
            $this->customers = DberpCustomer::query()
                ->whereIn('id', $customerIds)
                ->pluck('name', 'id')
                ->all();
        }

        if ($supplierIds !== []) {
            $this->suppliers = DberpSupplier::query()
                ->whereIn('id', $supplierIds)
                ->pluck('name', 'id')
                ->all();
        }
    }

    public function name(DberpSettlementWriteOff $record): string
    {
        $counterpartyId = (int) $record->counterparty_id;

        if ($record->counterparty_type === 'supplier') {
            return $this->suppliers[$counterpartyId] ?? (string) $record->counterparty_id;
        }

        return $this->customers[$counterpartyId] ?? (string) $record->counterparty_id;
    }
}
