<?php

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

class RemoveLegacyVarietyVersionColumns extends Migration
{
	/**
	 * Run the migrations.
	 */
	public function up(): void
	{
		// Drop legacy 'grape_variety_version' column from vineyard_rows if present
		if (Schema::hasColumn('vineyard_rows', 'grape_variety_version')) {
			Schema::table('vineyard_rows', function (Blueprint $table) {
				$table->dropColumn('grape_variety_version');
			});
		}

		// Drop legacy variety version columns from harvests if present
		if (Schema::hasColumn('harvests', 'variety_version')) {
			Schema::table('harvests', function (Blueprint $table) {
				$table->dropColumn('variety_version');
			});
		}

		if (Schema::hasColumn('harvests', 'grape_variety_version')) {
			Schema::table('harvests', function (Blueprint $table) {
				$table->dropColumn('grape_variety_version');
			});
		}
	}

	/**
	 * Reverse the migrations.
	 */
	public function down(): void
	{
		// Restore columns as nullable integers if rolling back (safe default)
		if (! Schema::hasColumn('vineyard_rows', 'grape_variety_version')) {
			Schema::table('vineyard_rows', function (Blueprint $table) {
				$table->integer('grape_variety_version')->nullable()->after('grape_variety_variant_id');
			});
		}

		if (! Schema::hasColumn('harvests', 'variety_version')) {
			Schema::table('harvests', function (Blueprint $table) {
				$table->integer('variety_version')->nullable()->after('grape_variety_variant_id');
			});
		}

		if (! Schema::hasColumn('harvests', 'grape_variety_version')) {
			Schema::table('harvests', function (Blueprint $table) {
				$table->integer('grape_variety_version')->nullable()->after('grape_variety_variant_id');
			});
		}
	}
}

