<?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::create('plan_permissions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('plan_id')->constrained()->onDelete('cascade');
            $table->foreignId('plan_feature_id')->constrained()->onDelete('cascade');
            $table->string('permission'); // e.g., 'can_export', 'can_use_api'
            $table->boolean('is_granted')->default(true);
            $table->json('conditions')->nullable(); // Additional conditions for permission
            $table->boolean('applies_during_trial')->default(true);
            $table->boolean('trial_permission_override')->nullable(); // Different trial permission
            $table->timestamps();

            $table->unique(['plan_id', 'plan_feature_id', 'permission']);
            $table->index(['plan_id', 'is_granted']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('plan_permissions');
    }
};
