<?php

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

return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('user_verification_tokens')) {
            return;
        }

        // Skip rename on SQLite (in-memory tests) — SQLite doesn't support CHANGE syntax.
        try {
            $driver = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
        } catch (\Throwable $e) {
            $driver = null;
        }

        if ($driver === 'sqlite') {
            return;
        }

        // Use raw statement to avoid requiring doctrine/dbal for simple rename
        // Adjust column definition to match original migration (VARCHAR(128) NOT NULL)
        DB::statement("ALTER TABLE `user_verification_tokens` CHANGE `token` `token_hash` VARCHAR(128) NOT NULL");
    }

    public function down(): void
    {
        if (! Schema::hasTable('user_verification_tokens')) {
            return;
        }

        try {
            $driver = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
        } catch (\Throwable $e) {
            $driver = null;
        }

        if ($driver === 'sqlite') {
            return;
        }

        DB::statement("ALTER TABLE `user_verification_tokens` CHANGE `token_hash` `token` VARCHAR(128) NOT NULL");
    }
};
