<?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
    {
        if (! Schema::hasTable('offboardings')) {
            return;
        }

        Schema::table('offboardings', function (Blueprint $table) {
            if (! Schema::hasColumn('offboardings', 'offboarding_date')) {
                $table->date('offboarding_date')->nullable()->index();
            }
            if (! Schema::hasColumn('offboardings', 'reason')) {
                $table->text('reason')->nullable();
            }
            if (! Schema::hasColumn('offboardings', 'notes')) {
                $table->text('notes')->nullable();
            }
        });

        $driver = Schema::getConnection()->getDriverName();
        if ($driver === 'mysql') {
            if (Schema::hasColumn('offboardings', 'start_date')) {
                DB::statement(
                    'UPDATE `offboardings` SET `offboarding_date` = DATE(`start_date`) WHERE `offboarding_date` IS NULL AND `start_date` IS NOT NULL'
                );
            } elseif (Schema::hasColumn('offboardings', 'submit_date')) {
                DB::statement(
                    'UPDATE `offboardings` SET `offboarding_date` = DATE(`submit_date`) WHERE `offboarding_date` IS NULL AND `submit_date` IS NOT NULL'
                );
            }

            if (Schema::hasColumn('offboardings', 'description')) {
                DB::statement(
                    'UPDATE `offboardings` SET `reason` = `description` WHERE (`reason` IS NULL OR `reason` = \'\') AND `description` IS NOT NULL'
                );
            }
            if (Schema::hasColumn('offboardings', 'reply_notes')) {
                DB::statement(
                    'UPDATE `offboardings` SET `notes` = `reply_notes` WHERE (`notes` IS NULL OR `notes` = \'\') AND `reply_notes` IS NOT NULL'
                );
            }

            // Legacy schema had required title/description; relax to avoid insert failures.
            if (Schema::hasColumn('offboardings', 'title')) {
                DB::statement('ALTER TABLE `offboardings` MODIFY `title` VARCHAR(191) NULL');
            }
            if (Schema::hasColumn('offboardings', 'description')) {
                DB::statement('ALTER TABLE `offboardings` MODIFY `description` TEXT NULL');
            }
        }
    }

    public function down(): void
    {
        if (! Schema::hasTable('offboardings')) {
            return;
        }

        Schema::table('offboardings', function (Blueprint $table) {
            if (Schema::hasColumn('offboardings', 'notes')) {
                $table->dropColumn('notes');
            }
            if (Schema::hasColumn('offboardings', 'reason')) {
                $table->dropColumn('reason');
            }
            if (Schema::hasColumn('offboardings', 'offboarding_date')) {
                $table->dropColumn('offboarding_date');
            }
        });
    }
};
