<?php
require '../element/connection.php';
require '../element/auth.php'; // Your admin auth check

$configFile = '../config/rewards_config.php';
$rewardsConfig = include $configFile;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newConfig = [
        'level_amounts' => [],
        'max_levels' => (int)$_POST['max_levels'],
        'level_names' => [],
        'users_per_level' => []
    ];
    
    for ($i = 1; $i <= $newConfig['max_levels']; $i++) {
        $newConfig['level_amounts'][$i] = (float)($_POST["level_$i"] ?? 0);
        $newConfig['level_names'][$i] = $_POST["name_$i"] ?? "Level $i";
        $newConfig['users_per_level'][$i] = (int)($_POST["users_$i"] ?? pow(4, $i));
    }
    
    // Save to config file
    file_put_contents($configFile, "<?php\nreturn " . var_export($newConfig, true) . ";\n?>");
    
    echo "<div class='alert alert-success'>✅ Rewards configuration updated successfully!</div>";
    $rewardsConfig = $newConfig;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Rewards Settings</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h2>Rewards Configuration</h2>
        <p class="text-muted">Easily edit level rewards and names</p>
        
        <form method="POST">
            <div class="row mb-3">
                <div class="col-md-4">
                    <label>Maximum Levels:</label>
                    <input type="number" name="max_levels" class="form-control" 
                           value="<?= $rewardsConfig['max_levels'] ?>" min="1" max="20">
                </div>
            </div>
            
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Level</th>
                        <th>Level Name</th>
                        <th>Reward Amount (₹)</th>
                        <th>Users Required</th>
                    </tr>
                </thead>
                <tbody>
                    <?php for ($i = 1; $i <= $rewardsConfig['max_levels']; $i++): ?>
                    <tr>
                        <td>Level <?= $i ?></td>
                        <td>
                            <input type="text" name="name_<?= $i ?>" class="form-control" 
                                   value="<?= $rewardsConfig['level_names'][$i] ?? "Level $i" ?>">
                        </td>
                        <td>
                            <input type="number" name="level_<?= $i ?>" class="form-control" 
                                   value="<?= $rewardsConfig['level_amounts'][$i] ?? 0 ?>" step="0.01">
                        </td>
                        <td>
                            <input type="number" name="users_<?= $i ?>" class="form-control" 
                                   value="<?= $rewardsConfig['users_per_level'][$i] ?? pow(4, $i) ?>" readonly 
                                   class="form-control bg-light">
                            <small class="text-muted">Auto-calculated: 4^<?= $i ?> = <?= pow(4, $i) ?> users</small>
                        </td>
                    </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
            
            <button type="submit" class="btn btn-primary">Save Configuration</button>
            <a href="test_rewards.php" class="btn btn-info">Test Rewards</a>
        </form>
        
        <div class="mt-4">
            <h4>How Level Completion Works:</h4>
            <ul>
                <li><strong>Level 1:</strong> All 4 direct children must have 4 children each (C1 status)</li>
                <li><strong>Level 2:</strong> All 16 users at level 2 must have 4 children each</li>
                <li><strong>Level 3:</strong> All 64 users at level 3 must have 4 children each</li>
                <li>And so on...</li>
            </ul>
        </div>
    </div>
</body>
</html>