<?php

declare(strict_types=1);

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

namespace App\Support;

use App\Filament\Support\DberpFilamentNumberFields;
use App\Models\DberpWarehouse;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;

/**
 * 单据明细：批量添加商品弹窗表单字段（订单/报价/申请/库存手工单等复用）。
 */
final class DberpDocumentLineBulkProductAddFormFields
{
    public const FIELD_PRODUCT_SKU_IDS = 'product_sku_ids';

    public const FIELD_DEFAULT_QUANTITY = 'default_quantity';

    public const FIELD_REMARK = 'remark';

    public const FIELD_SKIP_EXISTING_SKUS = 'skip_existing_skus';

    public const FIELD_WAREHOUSE_ID = 'warehouse_id';

    public static function productSkuMultiSelect(string $productScope): Select
    {
        return DberpProductSkuSelectOptions::configureProductSkuMultiSelect(
            Select::make(self::FIELD_PRODUCT_SKU_IDS)
                ->label(__('dberp.fields.bulk_add_product_skus'))
                ->helperText(__('dberp.hints.bulk_add_product_skus_search'))
                ->required(),
            $productScope,
        );
    }

    /**
     * @return list<Select|TextInput|Textarea|Toggle>
     */
    public static function modalFields(string $productScope): array
    {
        return array_merge(
            [self::productSkuMultiSelect($productScope)],
            self::sharedQuantityRemarkAndSkipFields(),
        );
    }

    /**
     * @return list<Select|TextInput|Textarea|Toggle>
     */
    public static function modalFieldsForPurchaseRequest(): array
    {
        return array_merge(
            [self::productSkuMultiSelect(DberpProductSkuSelectOptions::PRODUCT_SCOPE_PURCHASABLE)],
            [
                Select::make(self::FIELD_WAREHOUSE_ID)
                    ->label(__('dberp.fields.warehouse'))
                    ->options(fn (): array => DberpWarehouse::query()
                        ->where('is_enabled', true)
                        ->orderBy('sort_order')
                        ->orderBy('id')
                        ->pluck('name', 'id')
                        ->all())
                    ->default(fn (): ?int => DberpWarehouse::query()
                        ->where('is_enabled', true)
                        ->where('is_default', true)
                        ->value('id'))
                    ->searchable()
                    ->preload()
                    ->required(),
            ],
            self::sharedQuantityRemarkAndSkipFields(),
        );
    }

    /**
     * @return list<TextInput|Textarea|Toggle>
     */
    private static function sharedQuantityRemarkAndSkipFields(): array
    {
        return [
            DberpFilamentNumberFields::documentLineQuantityInput(
                TextInput::make(self::FIELD_DEFAULT_QUANTITY)
                    ->label(__('dberp.fields.bulk_add_default_quantity'))
                    ->default('1.0000')
                    ->required(),
                withHelperText: false,
                minValue: 0.0001,
            ),
            Textarea::make(self::FIELD_REMARK)
                ->label(__('dberp.fields.remark'))
                ->rows(2)
                ->columnSpanFull(),
            Toggle::make(self::FIELD_SKIP_EXISTING_SKUS)
                ->label(__('dberp.fields.bulk_add_skip_existing_skus'))
                ->default(false),
        ];
    }

    /**
     * @return list<int>
     */
    public static function normalizeSkuIds(mixed $raw): array
    {
        $ids = [];
        foreach ((array) $raw as $value) {
            $id = (int) $value;
            if ($id > 0) {
                $ids[] = $id;
            }
        }

        return array_values(array_unique($ids));
    }
}
