<?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 (DB::getDriverName() !== 'pgsql' || ! Schema::hasTable('subscriptions')) {
            return;
        }

        DB::statement('DROP INDEX IF EXISTS subscriptions_user_id_stripe_status_index');
        DB::statement('ALTER TABLE subscriptions ALTER COLUMN user_id TYPE CHAR(26) USING user_id::text');
        DB::statement('CREATE INDEX subscriptions_user_id_stripe_status_index ON subscriptions (user_id, stripe_status)');
    }

    public function down(): void
    {
        if (DB::getDriverName() !== 'pgsql' || ! Schema::hasTable('subscriptions')) {
            return;
        }

        DB::statement('DROP INDEX IF EXISTS subscriptions_user_id_stripe_status_index');
        DB::statement('ALTER TABLE subscriptions ALTER COLUMN user_id TYPE BIGINT USING user_id::bigint');
        DB::statement('CREATE INDEX subscriptions_user_id_stripe_status_index ON subscriptions (user_id, stripe_status)');
    }
};
