<?php

declare(strict_types=1);

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('subscriptions', function (Blueprint $table): void {
            $table->foreignId('plan_id')->nullable()->constrained()->after('type');
            $table->string('gateway')->default('stripe')->after('plan_id');
            $table->string('gateway_subscription_id')->nullable()->after('gateway');
            $table->string('gateway_customer_id')->nullable()->after('gateway_subscription_id');
            $table->string('gateway_price_id')->nullable()->after('gateway_customer_id');
            $table->timestamp('current_period_start')->nullable()->after('gateway_price_id');
            $table->timestamp('current_period_end')->nullable()->after('current_period_start');
            $table->timestamp('cancelled_at')->nullable()->after('current_period_end');
        });
    }

    public function down(): void
    {
        Schema::table('subscriptions', function (Blueprint $table): void {
            $table->dropForeign(['plan_id']);
            $table->dropColumn([
                'plan_id',
                'gateway',
                'gateway_subscription_id',
                'gateway_customer_id',
                'gateway_price_id',
                'current_period_start',
                'current_period_end',
                'cancelled_at',
            ]);
        });
    }
};
