<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        // 1) field_key 컬럼 추가
        Schema::table('document_template_basic_fields', function (Blueprint $table) {
            $table->string('field_key', 30)->nullable()->after('label')
                ->comment('common_codes(doc_template_basic_field) 참조 키');
        });

        // 2) common_codes에 doc_template_basic_field 코드그룹 시드
        $codes = [
            ['code' => 'product_name', 'name' => '품명', 'attributes' => json_encode(['data_path' => 'productName']), 'sort_order' => 1],
            ['code' => 'specification', 'name' => '규격', 'attributes' => json_encode(['data_path' => 'specification']), 'sort_order' => 2],
            ['code' => 'lot_no', 'name' => 'LOT NO', 'attributes' => json_encode(['data_path' => 'lotNo']), 'sort_order' => 3],
            ['code' => 'lot_size', 'name' => '로트크기', 'attributes' => json_encode(['data_path' => 'lotSize']), 'sort_order' => 4],
            ['code' => 'client', 'name' => '발주처/수주처', 'attributes' => json_encode(['data_path' => 'client']), 'sort_order' => 5],
            ['code' => 'site_name', 'name' => '현장명', 'attributes' => json_encode(['data_path' => 'projectName']), 'sort_order' => 6],
            ['code' => 'inspection_date', 'name' => '검사일자', 'attributes' => json_encode(['data_path' => '_inspection_date']), 'sort_order' => 7],
            ['code' => 'inspector', 'name' => '검사자', 'attributes' => json_encode(['data_path' => '_inspector']), 'sort_order' => 8],
        ];

        $now = now();
        foreach ($codes as $code) {
            DB::table('common_codes')->insert(array_merge($code, [
                'tenant_id' => 1,
                'code_group' => 'doc_template_basic_field',
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ]));
        }

        // 3) 기존 template 59 basic_fields에 field_key 매핑
        $labelKeyMap = [
            '품명' => 'product_name',
            '규격' => 'specification',
            '수주 LOT NO' => 'lot_no',
            '로트크기' => 'lot_size',
            '발주처' => 'client',
            '현장명' => 'site_name',
            '검사일자' => 'inspection_date',
            '검사자' => 'inspector',
        ];

        foreach ($labelKeyMap as $label => $fieldKey) {
            DB::table('document_template_basic_fields')
                ->where('template_id', 59)
                ->where('label', $label)
                ->update(['field_key' => $fieldKey]);
        }
    }

    public function down(): void
    {
        DB::table('common_codes')
            ->where('code_group', 'doc_template_basic_field')
            ->delete();

        Schema::table('document_template_basic_fields', function (Blueprint $table) {
            $table->dropColumn('field_key');
        });
    }
};
