# Core Concepts

Source: https://appfig.com/docs/concepts

Understanding these four concepts will help you master AppFig in minutes.

## 1. Features

**A feature is something you want to control remotely.**

Features have:
- A **name** (e.g., "double_xp", "max_lives", "welcome_message")
- A **value** (e.g., "on", "5", "Welcome back!")

```csharp
// Check if a feature is enabled (value == "on")
if (AppFig.IsFeatureEnabled("double_xp")) {
    ApplyDoubleXP();
}

// Get a feature's value
string maxLives = AppFig.GetFeatureValue("max_lives"); // Returns "5"
int lives = int.Parse(maxLives);
```

**Common feature types:**
- **Boolean flags**: "new_ui" = "on" or "off"
- **Numeric values**: "max_lives" = "5"
- **Text content**: "welcome_message" = "Hello!"
- **Complex data**: "pricing" = JSON object

## 2. Rules

**Rules determine when a feature should activate and what value it should have.**

A feature can have multiple rules. The **first matching rule** wins.

```json
Feature: "veteran_bonus"
  Rule 1: IF user completed 50+ levels → value = "3x"
  Rule 2: IF user completed 10+ levels → value = "2x"
  (No match) → feature returns null
```

**Rule structure:**
```json
{
  "conditions": {
    "events": [...],           // User actions
    "user_properties": [...],  // User attributes
    "device": [...]            // Device/platform info
  },
  "value": "on"                // What to return if conditions match
}
```

All three condition types must pass for a rule to match (AND logic between types).

## 3. Events

**Events are user actions your app logs.**

```csharp
// Simple event
AppFig.LogEvent("level_complete");

// Event with parameters
AppFig.LogEvent("purchase_made", new Dictionary<string, string> {
    { "item", "sword" },
    { "price", "4.99" }
});
```

**Rules can match based on:**
- **Count**: User completed 10+ levels
- **Recency**: User opened app in last 7 days
- **Parameters**: User purchased items priced over $10
- **Sequence**: User did A, then B, then C (in order)

**Example rule:**
```json
{
  "conditions": {
    "events": {
      "mode": "simple",
      "operator": "AND",
      "items": [
        {
          "key": "level_complete",
          "count": {
            "operator": ">=",
            "value": 10
          },
          "within_last_days": 7
        }
      ]
    }
  },
  "value": "on"
}
```

This matches if user completed 10+ levels in the last 7 days.

## 4. Properties

**Properties are attributes that describe users and devices.**

### User Properties

Set by your app to describe the user:

```csharp
AppFig.SetUserProperty("subscription_tier", "premium");
AppFig.SetUserProperty("account_age_days", "45");
AppFig.SetUserProperty("preferred_language", "en");
```

**Example rule:**
```json
{
  "conditions": {
    "user_properties": [
      {
        "key": "subscription_tier",
        "value": {
          "operator": "==",
          "value": "premium"
        }
      }
    ]
  },
  "value": "on"
}
```

### Device Properties

Auto-detected by the SDK (no manual setup):

```
platform: "iOS", "Android", "Windows", etc.
os_version: "15.0", "13", "11"
language: "en-US", "ja", "es"
country: "US", "JP", "GB"
device_brand: "Apple", "Samsung", "Google"
device_model: "iPhone 14", "Galaxy S23"
```

**Example rule:**
```json
{
  "conditions": {
    "device": [
      {
        "key": "platform",
        "value": {
          "operator": "in",
          "value": ["iOS", "Android"]
        }
      }
    ]
  },
  "value": "on"
}
```

This enables the feature only on mobile platforms.

## How They Work Together

```
1. Your app logs events and sets user properties

   AppFig.LogEvent("level_complete")
   AppFig.SetUserProperty("subscription_tier", "free")

2. AppFig evaluates all rules for a feature

   Feature: "veteran_bonus"

   Rule 1: Check conditions
     ✓ Events: level_complete count >= 10? YES
     ✓ User properties: subscription_tier == "premium"? NO
     ✗ Rule 1 fails

   Rule 2: Check conditions
     ✓ Events: level_complete count >= 5? YES
     ✓ User properties: None required
     ✓ Device: None required
     ✅ Rule 2 passes!

3. First matching rule returns its value

   AppFig.GetFeatureValue("veteran_bonus") // Returns "1.5x"
```

## Key Principles

### 1. Client-Side Evaluation
Rules are evaluated **on your device**, not on a server. This means:
- ✅ Zero latency (no network calls)
- ✅ Works offline
- ✅ No sensitive data sent to servers

### 2. First Match Wins
When multiple rules could match, the **first one wins**. Order matters!

```json
Feature: "discount"
  Rule 1: IF premium user → "50% off"
  Rule 2: IF any user → "10% off"

Premium users will get 50% (Rule 1 matches first)
Free users will get 10% (Rule 1 doesn't match, Rule 2 does)
```

### 3. Null vs Empty String
If no rules match, `GetFeatureValue()` returns `null` (not an empty string).

```csharp
string value = AppFig.GetFeatureValue("veteran_bonus");
if (value == null) {
    // Feature has no matching rules
}
```

### 4. All Conditions Must Pass
Within a rule, ALL condition types must pass:
- Events AND
- User Properties AND
- Device Properties

But within each type, you can use AND or OR operators.

## Next Steps

- **[Quick Start Guide](https://appfig.com/docs/quick-start-guide)** - Set up your first feature
- **[How It Works](https://appfig.com/docs/how-it-works)** - Deep dive into rule evaluation
- **[Rule Schema Reference](https://appfig.com/docs/rule-schema)** - All operators and options

## Examples

### Example 1: Welcome Bonus for New Users
```csharp
// Rule: Show welcome bonus to users who opened app < 3 times
AppFig.LogEvent("app_open");
if (AppFig.IsFeatureEnabled("welcome_bonus")) {
    ShowWelcomeBonus();
}
```

### Example 2: Platform-Specific UI
```csharp
// Rule: Show mobile controls only on mobile
// Device property "platform" is auto-detected
if (AppFig.IsFeatureEnabled("mobile_controls")) {
    EnableTouchControls();
}
```

### Example 3: Subscription-Based Content
```csharp
// Set user property once after checking subscription
AppFig.SetUserProperty("subscription_tier", "premium");

// Rule: Show premium content only to premium users
if (AppFig.IsFeatureEnabled("premium_content")) {
    ShowPremiumLevels();
}
```

### Example 4: Engagement-Based Unlocks
```csharp
// Log events as user plays
AppFig.LogEvent("level_complete");
AppFig.LogEvent("level_complete");
// ... 10 times total

// Rule: Unlock hard mode after 10 level completions
if (AppFig.IsFeatureEnabled("hard_mode")) {
    UnlockHardMode();
}
```
