<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DocumentTemplateFieldPresetSeeder extends Seeder
{
    public function run(): void
    {
        $now = now();

        DB::table('document_template_field_presets')->truncate();

        // 수입검사 프리셋
        DB::table('document_template_field_presets')->insert([
            'name' => '수입검사 기본 필드 세트',
            'category' => '수입검사',
            'fields' => json_encode([
                ['field_key' => 'category', 'label' => '구분', 'field_type' => 'text', 'width' => '65px', 'is_required' => false],
                ['field_key' => 'item', 'label' => '검사항목', 'field_type' => 'text', 'width' => '130px', 'is_required' => true],
                ['field_key' => 'standard', 'label' => '검사 기준/범위', 'field_type' => 'text_with_criteria', 'width' => '220px', 'is_required' => false],
                ['field_key' => 'tolerance', 'label' => '공차/범위', 'field_type' => 'json_tolerance', 'width' => '85px', 'is_required' => false],
                ['field_key' => 'method', 'label' => '검사방식', 'field_type' => 'select_api', 'width' => '110px', 'is_required' => false, 'options' => [
                    'api_endpoint' => '/api/admin/common-codes/inspection_method',
                    'auto_map' => [
                        'target_field' => 'measurement_type',
                        'mapping' => [
                            'visual' => 'checkbox',
                            'check' => 'numeric',
                            'mill_sheet' => 'single_value',
                            'certified_agency' => 'single_value',
                            'substitute_cert' => 'substitute',
                            'other' => 'text',
                        ],
                    ],
                ]],
                ['field_key' => 'measurement_type', 'label' => '측정유형', 'field_type' => 'select', 'width' => '100px', 'is_required' => false, 'options' => [
                    'choices' => [
                        ['code' => 'checkbox', 'name' => 'OK/NG 체크'],
                        ['code' => 'numeric', 'name' => '수치입력(3)'],
                        ['code' => 'single_value', 'name' => '단일값'],
                        ['code' => 'substitute', 'name' => '성적서 대체'],
                        ['code' => 'text', 'name' => '자유입력'],
                    ],
                ]],
                ['field_key' => 'frequency', 'label' => '검사주기', 'field_type' => 'composite_frequency', 'width' => '120px', 'is_required' => false],
                ['field_key' => 'regulation', 'label' => '관련규정', 'field_type' => 'text', 'width' => '80px', 'is_required' => false],
            ], JSON_UNESCAPED_UNICODE),
            'links' => json_encode([
                ['link_key' => 'items', 'label' => '연결 품목 (RM, SM)', 'link_type' => 'multiple', 'source_table' => 'items', 'search_params' => ['item_type' => 'RM,SM'], 'display_fields' => ['title' => 'name', 'subtitle' => 'code']],
            ], JSON_UNESCAPED_UNICODE),
            'sort_order' => 0,
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        // 품질검사 프리셋
        DB::table('document_template_field_presets')->insert([
            'name' => '품질검사 기본 필드 세트',
            'category' => '품질검사',
            'fields' => json_encode([
                ['field_key' => 'category', 'label' => '구분', 'field_type' => 'text', 'width' => '65px', 'is_required' => false],
                ['field_key' => 'item', 'label' => '검사항목', 'field_type' => 'text', 'width' => '130px', 'is_required' => true],
                ['field_key' => 'standard', 'label' => '검사 기준/범위', 'field_type' => 'text_with_criteria', 'width' => '220px', 'is_required' => false],
                ['field_key' => 'tolerance', 'label' => '공차/범위', 'field_type' => 'json_tolerance', 'width' => '85px', 'is_required' => false],
                ['field_key' => 'method', 'label' => '검사방식', 'field_type' => 'select_api', 'width' => '110px', 'is_required' => false, 'options' => [
                    'api_endpoint' => '/api/admin/common-codes/inspection_method',
                    'auto_map' => [
                        'target_field' => 'measurement_type',
                        'mapping' => [
                            'visual' => 'checkbox',
                            'check' => 'numeric',
                            'mill_sheet' => 'single_value',
                            'certified_agency' => 'single_value',
                            'substitute_cert' => 'substitute',
                            'other' => 'text',
                        ],
                    ],
                ]],
                ['field_key' => 'measurement_type', 'label' => '측정유형', 'field_type' => 'select', 'width' => '100px', 'is_required' => false, 'options' => [
                    'choices' => [
                        ['code' => 'checkbox', 'name' => 'OK/NG 체크'],
                        ['code' => 'numeric', 'name' => '수치입력(3)'],
                        ['code' => 'single_value', 'name' => '단일값'],
                        ['code' => 'substitute', 'name' => '성적서 대체'],
                        ['code' => 'text', 'name' => '자유입력'],
                    ],
                ]],
                ['field_key' => 'frequency', 'label' => '검사주기', 'field_type' => 'composite_frequency', 'width' => '120px', 'is_required' => false],
                ['field_key' => 'regulation', 'label' => '관련규정', 'field_type' => 'text', 'width' => '80px', 'is_required' => false],
            ], JSON_UNESCAPED_UNICODE),
            'links' => json_encode([
                ['link_key' => 'process', 'label' => '연결 공정', 'link_type' => 'single', 'source_table' => 'processes', 'search_params' => null, 'display_fields' => ['title' => 'process_name', 'subtitle' => 'process_code']],
            ], JSON_UNESCAPED_UNICODE),
            'sort_order' => 1,
            'created_at' => $now,
            'updated_at' => $now,
        ]);

        // 출하검사 프리셋
        DB::table('document_template_field_presets')->insert([
            'name' => '출하검사 기본 필드 세트',
            'category' => '출하검사',
            'fields' => json_encode([
                ['field_key' => 'category', 'label' => '구분', 'field_type' => 'text', 'width' => '65px', 'is_required' => false],
                ['field_key' => 'item', 'label' => '검사항목', 'field_type' => 'text', 'width' => '130px', 'is_required' => true],
                ['field_key' => 'standard', 'label' => '검사 기준/범위', 'field_type' => 'text_with_criteria', 'width' => '220px', 'is_required' => false],
                ['field_key' => 'tolerance', 'label' => '공차/범위', 'field_type' => 'json_tolerance', 'width' => '85px', 'is_required' => false],
                ['field_key' => 'method', 'label' => '검사방식', 'field_type' => 'select_api', 'width' => '110px', 'is_required' => false, 'options' => [
                    'api_endpoint' => '/api/admin/common-codes/inspection_method',
                    'auto_map' => [
                        'target_field' => 'measurement_type',
                        'mapping' => [
                            'visual' => 'checkbox',
                            'check' => 'numeric',
                            'mill_sheet' => 'single_value',
                            'certified_agency' => 'single_value',
                            'substitute_cert' => 'substitute',
                            'other' => 'text',
                        ],
                    ],
                ]],
                ['field_key' => 'measurement_type', 'label' => '측정유형', 'field_type' => 'select', 'width' => '100px', 'is_required' => false, 'options' => [
                    'choices' => [
                        ['code' => 'checkbox', 'name' => 'OK/NG 체크'],
                        ['code' => 'numeric', 'name' => '수치입력(3)'],
                        ['code' => 'single_value', 'name' => '단일값'],
                        ['code' => 'substitute', 'name' => '성적서 대체'],
                        ['code' => 'text', 'name' => '자유입력'],
                    ],
                ]],
                ['field_key' => 'frequency', 'label' => '검사주기', 'field_type' => 'composite_frequency', 'width' => '120px', 'is_required' => false],
                ['field_key' => 'regulation', 'label' => '관련규정', 'field_type' => 'text', 'width' => '80px', 'is_required' => false],
            ], JSON_UNESCAPED_UNICODE),
            'links' => json_encode([
                ['link_key' => 'lot', 'label' => '로트번호', 'link_type' => 'single', 'source_table' => 'lots', 'search_params' => null, 'display_fields' => ['title' => 'lot_no', 'subtitle' => 'item_name']],
            ], JSON_UNESCAPED_UNICODE),
            'sort_order' => 2,
            'created_at' => $now,
            'updated_at' => $now,
        ]);
    }
}
