# Feature Flags Guide

## Overview
Feature flags allow you to enable/disable features dynamically without deploying new code.

## Creating Feature Flags

### Via Database
```sql
INSERT INTO feature_flags (key, name, description, is_enabled, rollout_percentage)
VALUES ('new_checkout', 'New Checkout Flow', 'New improved checkout experience', true, 50);
```

### Via Seeder
```php
FeatureFlag::create([
    'key' => 'new_checkout',
    'name' => 'New Checkout Flow',
    'description' => 'New improved checkout experience',
    'is_enabled' => true,
    'rollout_percentage' => 50, // 50% of users
]);
```

## Usage

### In Controllers
```php
use App\Services\FeatureFlagService;

public function checkout(FeatureFlagService $featureFlagService)
{
    if ($featureFlagService->isEnabled('new_checkout', auth()->id())) {
        return view('checkout.new');
    }
    
    return view('checkout.old');
}
```

### In Blade Templates
```blade
@if(feature_enabled('new_checkout'))
    <div class="new-feature">New Feature</div>
@else
    <div class="old-feature">Old Feature</div>
@endif
```

### In Routes (Middleware)
```php
Route::get('/new-checkout', [CheckoutController::class, 'newCheckout'])
    ->middleware('feature:new_checkout');
```

## Configuration Options

- **is_enabled**: Enable/disable the flag
- **rollout_percentage**: Percentage of users to enable (0-100)
- **target_users**: Array of specific user IDs
- **starts_at**: When to start the feature
- **ends_at**: When to end the feature
- **metadata**: Additional configuration data

## Examples

### Gradual Rollout
```php
FeatureFlag::create([
    'key' => 'new_feature',
    'is_enabled' => true,
    'rollout_percentage' => 25, // Start with 25%
]);
```

### User-Specific
```php
FeatureFlag::create([
    'key' => 'beta_feature',
    'is_enabled' => true,
    'target_users' => [1, 2, 3], // Only for specific users
]);
```

### Time-Limited
```php
FeatureFlag::create([
    'key' => 'promo_feature',
    'is_enabled' => true,
    'starts_at' => '2025-12-01 00:00:00',
    'ends_at' => '2025-12-31 23:59:59',
]);
```

