<?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('plans', function (Blueprint $table) {
            $table->foreignId('plan_tier_id')->nullable()->after('details')->constrained()->onDelete('set null');
            $table->integer('billing_cycle_days')->nullable()->after('plan_tier_id'); // 30, 90, 365, custom
            $table->boolean('is_active')->default(true)->after('billing_cycle_days');
            $table->integer('sort_order')->default(0)->after('is_active');
            $table->json('metadata')->nullable()->after('sort_order');

            $table->index(['is_active', 'sort_order']);
            $table->index(['plan_tier_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('plans', function (Blueprint $table) {
            $table->dropForeign(['plan_tier_id']);
            $table->dropColumn([
                'plan_tier_id',
                'billing_cycle_days',
                'is_active',
                'sort_order',
                'metadata',
            ]);
        });
    }
};
