<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Detect DB driver
        $driver = Schema::getConnection()->getDriverName();

        // --- MySQL / MariaDB only ---
        // Drop JSON check constraints if they exist (safely)
        if (in_array($driver, ['mysql', 'mariadb'])) {
            try {
                // MySQL automatically names check constraints in different ways,
                // so we attempt a generic drop and ignore failures.
                DB::statement("ALTER TABLE `payment_providers` DROP CHECK `payment_providers.configuration`;");
            } catch (\Throwable $e) {
                // ignore if constraint does not exist
            }
        }

        // --- PostgreSQL ---
        // PostgreSQL may also require dropping check constraints but constraint names are random.
        // We try to drop any constraint *that checks JSON*.
        if ($driver === 'pgsql') {
            try {
                $constraint = DB::selectOne("
                    SELECT conname
                    FROM pg_constraint
                    JOIN pg_class ON pg_constraint.conrelid = pg_class.oid
                    WHERE relname = 'payment_providers'
                    AND pg_get_constraintdef(pg_constraint.oid) LIKE '%configuration%json%';
                ");

                if (!empty($constraint->conname)) {
                    DB::statement("ALTER TABLE payment_providers DROP CONSTRAINT {$constraint->conname};");
                }
            } catch (\Throwable $e) {
                // ignore if constraint does not exist
            }
        }

        // --- SQLite & SQL Server ---
        // No action needed:
        // SQLite doesn’t enforce JSON CHECK constraints
        // SQL Server does not create JSON CHECK constraints automatically.

        Schema::table('payment_providers', function (Blueprint $table) {
            // Replace JSON type with LONGTEXT / TEXT depending on driver
            // Laravel automatically maps this properly for each database.
            $table->longText('configuration')->change();
        });
    }

    public function down(): void
    {
        Schema::table('payment_providers', function (Blueprint $table) {
            // revert to JSON column
            // Laravel maps this to appropriate driver types
            $table->json('configuration')->change();
        });

        // Optionally: you can restore JSON CHECK constraints here, but it's not required
    }
};
