<?php

declare(strict_types=1);

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

namespace App\Support;

use App\Models\DberpDictionaryItem;
use Illuminate\Validation\ValidationException;

final class DberpPriceLevelDictionary
{
    public const TYPE_CODE = 'price_level';

    /**
     * @return array<string, string>
     */
    public static function optionsForSelect(?string $preserveCode = null): array
    {
        return DberpDictionaryItem::query()
            ->whereHas('type', fn ($query) => $query->where('code', self::TYPE_CODE))
            ->where(function ($query) use ($preserveCode): void {
                $query->where('is_enabled', true);

                if ($preserveCode !== null && $preserveCode !== '') {
                    $query->orWhere('code', $preserveCode);
                }
            })
            ->orderBy('sort_order')
            ->orderBy('id')
            ->pluck('label', 'code')
            ->all();
    }

    public static function labelForCode(?string $code): ?string
    {
        if ($code === null || $code === '') {
            return null;
        }

        $label = self::codeLabelMap()[$code] ?? null;

        return $label !== null && $label !== '' ? $label : null;
    }

    public static function displayLabel(?string $code): string
    {
        if ($code === null || $code === '') {
            return '';
        }

        $label = self::labelForCode($code);

        if ($label !== null) {
            return $label;
        }

        return $code.' ('.(string) __('dberp.hints.price_level_unknown').')';
    }

    public static function isEnabledCode(?string $code): bool
    {
        if ($code === null || $code === '') {
            return false;
        }

        return DberpDictionaryItem::query()
            ->whereHas('type', fn ($query) => $query->where('code', self::TYPE_CODE))
            ->where('code', $code)
            ->where('is_enabled', true)
            ->exists();
    }

    public static function assertEnabledCode(?string $code, string $attribute = 'price_level_code'): void
    {
        if ($code === null || $code === '') {
            return;
        }

        if (! self::isEnabledCode($code)) {
            throw ValidationException::withMessages([
                $attribute => (string) __('dberp.errors.price_level_code_invalid'),
            ]);
        }
    }

    /**
     * @return array<string, string>
     */
    private static function codeLabelMap(): array
    {
        return once(function (): array {
            return DberpDictionaryItem::query()
                ->whereHas('type', fn ($query) => $query->where('code', self::TYPE_CODE))
                ->orderBy('sort_order')
                ->orderBy('id')
                ->pluck('label', 'code')
                ->all();
        });
    }
}
