<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('import_mappings')) {
            Schema::create('import_mappings', function (Blueprint $table) {
                $table->id();
                $table->foreignId('import_id')->constrained('imports')->cascadeOnDelete();

                $table->string('source_column', 255);
                $table->string('target_field', 255)->nullable();
                $table->string('transform', 50)->nullable();
                $table->json('options')->nullable();

                $table->index(['import_id', 'source_column']);
                $table->index(['import_id', 'target_field']);

                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('import_mappings')) {
            Schema::drop('import_mappings');
        }
    }
};
