<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('item_fields', function (Blueprint $table) {
            // 내부용 매핑 컬럼 (API 응답에서 제외)
            $table->string('source_table', 100)
                ->nullable()
                ->after('properties')
                ->comment('내부용: 원본 테이블명 (products, materials 등)');

            $table->string('source_column', 100)
                ->nullable()
                ->after('source_table')
                ->comment('내부용: 원본 컬럼명 (code, name 등)');

            $table->enum('storage_type', ['column', 'json'])
                ->default('json')
                ->after('source_column')
                ->comment('내부용: 저장방식 (column=DB컬럼, json=attributes/options)');

            $table->string('json_path', 200)
                ->nullable()
                ->after('storage_type')
                ->comment('내부용: JSON 저장 경로 (예: attributes.custom_size)');

            // 인덱스
            $table->index(['source_table', 'source_column'], 'idx_source_mapping');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('item_fields', function (Blueprint $table) {
            $table->dropIndex('idx_source_mapping');
            $table->dropColumn(['source_table', 'source_column', 'storage_type', 'json_path']);
        });
    }
};
