<?php

declare(strict_types=1);

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

namespace App\Business\DataImport;

use App\Business\DataImport\Enums\DataImportType;
use App\Business\DataImport\Support\DataImportValueParser;

final class DataImportTemplateSamples
{
    public const ROW_PREFIX = '__example__';

    /**
     * @param  array<string, string|null>  $values
     */
    public function isExampleRow(DataImportType|DataImportTypeReference|string $type, array $values): bool
    {
        $reference = DataImportTypeReference::from($type);
        $enum = $reference->toEnum();

        $identifierField = match ($enum) {
            DataImportType::ProductBrand => 'brand_code',
            DataImportType::OpeningStock => 'external_key',
            null => app(DataImportDefinitionRegistry::class)->get($reference->key)->exampleRowIdentifierField ?? 'code',
            default => 'code',
        };

        $value = DataImportValueParser::trimmed($values[$identifierField] ?? null);

        if ($value === null) {
            return false;
        }

        return str_starts_with(strtolower($value), strtolower(self::ROW_PREFIX));
    }

    /**
     * @return list<list<string|null>>
     */
    public function rowsForExport(DataImportType|DataImportTypeReference|string $type, DataImportTemplateRegistry $registry, ?string $locale = null): array
    {
        $locale ??= app()->getLocale();
        $reference = DataImportTypeReference::from($type);
        $enum = $reference->toEnum();

        if ($enum === null) {
            $definition = app(DataImportDefinitionRegistry::class)->get($reference->key);

            if ($definition->sampleRowsResolver !== null) {
                return ($definition->sampleRowsResolver)($locale);
            }

            return [];
        }

        $headings = $registry->headings($enum);

        return array_map(
            function (array $row) use ($headings): array {
                return array_map(
                    static fn (string $fieldKey): ?string => $row[$fieldKey] ?? null,
                    $headings,
                );
            },
            $this->rowsByFieldKey($enum, $locale),
        );
    }

    /**
     * @return list<array<string, string|null>>
     */
    private function rowsByFieldKey(DataImportType $type, string $locale): array
    {
        $remark = (string) __('dberp.data_import.template_samples.example_row_remark', [], $locale);

        return match ($type) {
            DataImportType::Unit => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.unit_1_name', [], $locale),
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
                [
                    'code' => self::ROW_PREFIX.'002',
                    'name' => (string) __('dberp.data_import.template_samples.unit_2_name', [], $locale),
                    'is_enabled' => '1',
                    'sort_order' => '20',
                    'remark' => '',
                ],
            ],
            DataImportType::ProductCategory => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.category_1_name', [], $locale),
                    'parent_code' => '',
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
                [
                    'code' => self::ROW_PREFIX.'002',
                    'name' => (string) __('dberp.data_import.template_samples.category_2_name', [], $locale),
                    'parent_code' => self::ROW_PREFIX.'001',
                    'is_enabled' => '1',
                    'sort_order' => '20',
                    'remark' => '',
                ],
            ],
            DataImportType::ProductBrand => [
                [
                    'brand_code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.brand_1_name', [], $locale),
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
            ],
            DataImportType::CustomerCategory, DataImportType::SupplierCategory => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.partner_category_1_name', [], $locale),
                    'status' => 'active',
                    'sort' => '10',
                    'remark' => $remark,
                ],
            ],
            DataImportType::Warehouse => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.warehouse_1_name', [], $locale),
                    'warehouse_type' => 'general',
                    'is_default' => '1',
                    'manager_employee_code' => '',
                    'region_code' => '',
                    'address' => (string) __('dberp.data_import.template_samples.sample_address', [], $locale),
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
            ],
            DataImportType::Customer => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.customer_1_name', [], $locale),
                    'customer_category_code' => self::ROW_PREFIX.'001',
                    'settlement_method' => 'cash',
                    'contact_person' => (string) __('dberp.data_import.template_samples.sample_contact', [], $locale),
                    'mobile' => '13800138000',
                    'phone' => '',
                    'email' => 'customer@example.com',
                    'region_code' => '',
                    'address' => (string) __('dberp.data_import.template_samples.sample_address', [], $locale),
                    'price_level_code' => '',
                    'default_payment_term_days' => '30',
                    'credit_limit_amount' => '10000',
                    'salesperson_employee_code' => '',
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
            ],
            DataImportType::Supplier => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.supplier_1_name', [], $locale),
                    'supplier_category_code' => self::ROW_PREFIX.'001',
                    'settlement_method' => 'cash',
                    'contact_person' => (string) __('dberp.data_import.template_samples.sample_contact', [], $locale),
                    'mobile' => '13900139000',
                    'phone' => '021-88886666',
                    'email' => 'supplier@example.com',
                    'region_code' => '',
                    'address' => (string) __('dberp.data_import.template_samples.sample_address', [], $locale),
                    'is_enabled' => '1',
                    'sort_order' => '10',
                    'remark' => $remark,
                ],
            ],
            DataImportType::Product => [
                [
                    'code' => self::ROW_PREFIX.'001',
                    'name' => (string) __('dberp.data_import.template_samples.product_1_name', [], $locale),
                    'category_code' => self::ROW_PREFIX.'001',
                    'brand_code' => self::ROW_PREFIX.'001',
                    'spec_model' => (string) __('dberp.data_import.template_samples.sample_spec_model', [], $locale),
                    'unit_code' => self::ROW_PREFIX.'001',
                    'barcode' => '690000000001',
                    'cost_price' => '10.50',
                    'sales_price' => '15.00',
                    'manages_inventory' => '1',
                    'is_enabled' => '1',
                    'remark' => $remark,
                ],
            ],
            DataImportType::OpeningStock => [
                [
                    'external_key' => self::ROW_PREFIX.'001',
                    'product_code' => self::ROW_PREFIX.'001',
                    'sku_code' => '',
                    'warehouse_code' => self::ROW_PREFIX.'001',
                    'quantity' => '100',
                    'unit_cost' => '10.50',
                    'safety_stock' => '20',
                    'batch_no' => '',
                    'serial_no' => '',
                    'production_date' => '',
                    'expiry_date' => '',
                    'business_time' => '',
                    'remark' => $remark,
                ],
                [
                    'external_key' => self::ROW_PREFIX.'002',
                    'product_code' => self::ROW_PREFIX.'001',
                    'sku_code' => '',
                    'warehouse_code' => self::ROW_PREFIX.'001',
                    'quantity' => '50',
                    'unit_cost' => '10.50',
                    'safety_stock' => '',
                    'batch_no' => '',
                    'serial_no' => '',
                    'production_date' => '',
                    'expiry_date' => '',
                    'business_time' => '',
                    'remark' => '',
                ],
            ],
        };
    }
}
