<?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
    {
        try {
            Schema::create('feature_flags', function (Blueprint $table) {
                $table->id();
                $table->string('key', 191)->unique();
                $table->string('name', 255);
                $table->text('description')->nullable();
                $table->boolean('is_enabled')->default(false);
                $table->json('target_users')->nullable(); // User IDs or user groups
                $table->decimal('rollout_percentage', 5, 2)->default(0); // 0-100
                $table->timestamp('starts_at')->nullable();
                $table->timestamp('ends_at')->nullable();
                $table->json('metadata')->nullable(); // Additional configuration
                $table->timestamps();

                // 'key' already has unique index, no need for separate index
                $table->index('is_enabled');
                $table->index('starts_at');
                $table->index('ends_at');
            });
        } catch (\Exception $e) {
            \Log::error('Feature flags migration error: ' . $e->getMessage());
            throw $e;
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('feature_flags');
    }
};
