<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->string('package_id')->nullable()->after('user_id');
            $table->string('package_name')->nullable()->after('package_id');
            $table->string('interval')->default('month')->after('currency');
            $table->string('stripe_customer_id')->nullable()->after('gateway_subscription_id');
            $table->string('stripe_subscription_id')->nullable()->after('stripe_customer_id');
            $table->string('stripe_session_id')->nullable()->after('stripe_subscription_id');
            $table->timestamp('activated_at')->nullable()->after('current_period_end');
            $table->timestamp('expires_at')->nullable()->after('cancelled_at');
        });

        // Update status enum to include 'pending'
        DB::statement("ALTER TABLE `subscriptions` MODIFY `status` ENUM('pending', 'active', 'cancelled', 'expired', 'past_due') DEFAULT 'pending'");
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->dropColumn(['package_id', 'package_name', 'interval', 'stripe_customer_id', 'stripe_subscription_id', 'stripe_session_id', 'activated_at', 'expires_at']);
        });

        // Revert status enum
        DB::statement("ALTER TABLE `subscriptions` MODIFY `status` ENUM('active', 'cancelled', 'expired', 'past_due') DEFAULT 'active'");
    }
};
