<?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('products', function (Blueprint $table) {
            if (!Schema::hasColumn('products', 'base_product_version_id')) {
                $table->foreignId('base_product_version_id')
                    ->nullable()
                    ->after('category_id')
                    ->constrained('product_versions')
                    ->nullOnDelete();
            }
            if (!Schema::hasColumn('products', 'version_info')) {
                $table->text('version_info')
                    ->nullable()
                    ->after('base_product_version_id');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            if (Schema::hasColumn('products', 'base_product_version_id')) {
                $table->dropForeign(['base_product_version_id']);
                $table->dropColumn('base_product_version_id');
            }
        });
        Schema::table('products', function (Blueprint $table) {
            if (Schema::hasColumn('products', 'version_info')) {
                $table->dropColumn('version_info');
            }
        });
    }
};
