<?php

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

/**
 * Plans are the source of truth for what each tier unlocks. The `features`
 * JSON keeps gating data structured + extensible without piling boolean
 * columns onto the table every time a new capability ships:
 *
 *   {
 *     "can_export": bool,
 *     "project_limit": int|null    // null = unlimited
 *   }
 *
 * `(is_active, position)` is indexed because the public pricing page reads
 * it on every render and the query never filters by anything else.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('plans', function (Blueprint $table) {
            $table->id();
            $table->string('key', 32)->unique();
            $table->string('name', 64);
            $table->text('description')->nullable();
            $table->string('currency', 3);
            $table->unsignedInteger('monthly_price_cents');
            $table->unsignedInteger('yearly_price_cents');
            $table->json('features');
            $table->boolean('is_active')->default(true);
            $table->smallInteger('position');
            $table->unsignedInteger('max_seats')->nullable();
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('plans');
    }
};
