<?php

declare(strict_types=1);

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 $type, array $values): bool
    {
        $identifierField = match ($type) {
            DataImportType::ProductBrand => 'brand_code',
            DataImportType::OpeningStock => 'external_key',
            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 $type, DataImportTemplateRegistry $registry, ?string $locale = null): array
    {
        $locale ??= app()->getLocale();
        $headings = $registry->headings($type);

        return array_map(
            function (array $row) use ($headings): array {
                return array_map(
                    static fn (string $fieldKey): ?string => $row[$fieldKey] ?? null,
                    $headings,
                );
            },
            $this->rowsByFieldKey($type, $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' => '',
                ],
            ],
        };
    }
}
