<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('platform_subscriptions', function (Blueprint $table) {
            $table->foreignId('subscription_plan_id')
                ->nullable()
                ->after('association_id')
                ->constrained('subscription_plans')
                ->nullOnDelete();

            $table->enum('payment_method', ['card', 'bank_transfer'])
                ->default('card')
                ->after('payment_reference');

            $table->timestamp('trial_ends_at')
                ->nullable()
                ->after('ends_at');
        });

        // Make the legacy 'plan' column nullable for backwards compatibility
        Schema::table('platform_subscriptions', function (Blueprint $table) {
            $table->string('plan')->nullable()->change();
        });
    }

    public function down(): void
    {
        Schema::table('platform_subscriptions', function (Blueprint $table) {
            $table->dropForeign(['subscription_plan_id']);
            $table->dropColumn(['subscription_plan_id', 'payment_method', 'trial_ends_at']);
        });

        Schema::table('platform_subscriptions', function (Blueprint $table) {
            $table->enum('plan', ['basic', 'professional', 'enterprise'])->default('basic')->change();
        });
    }
};
