<?php
require_once 'header.php';

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $db = getDB();
    
    if (isset($_POST['add_habit'])) {
        // Handle habit image upload
        $imagePath = null;
        if (!empty($_FILES['habit_image']['tmp_name'])) {
            $uploadDir = IMAGE_PATH . '/habits/';
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            
            $filename = uniqid() . '_' . basename($_FILES['habit_image']['name']);
            $targetFile = $uploadDir . $filename;
            
            if (move_uploaded_file($_FILES['habit_image']['tmp_name'], $targetFile)) {
                $imagePath = IMAGE_URL . '/habits/' . $filename;
            }
        }
        
        $stmt = $db->prepare("INSERT INTO habits (name, description, time_of_day, frequency, xp_reward, show_on_calendar, image_path) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([
            $_POST['habit_name'],
            $_POST['habit_desc'],
            $_POST['habit_time'],
            $_POST['habit_freq'],
            $_POST['habit_xp'],
            isset($_POST['habit_calendar']) ? 1 : 0,
            $imagePath
        ]);
        $message = "Quest created successfully!";
    }
    
    if (isset($_POST['add_item'])) {
        // Handle item image upload
        $imagePath = null;
        if (!empty($_FILES['item_image']['tmp_name'])) {
            $uploadDir = IMAGE_PATH . '/items/';
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            
            $filename = uniqid() . '_' . basename($_FILES['item_image']['name']);
            $targetFile = $uploadDir . $filename;
            
            if (move_uploaded_file($_FILES['item_image']['tmp_name'], $targetFile)) {
                $imagePath = IMAGE_URL . '/items/' . $filename;
            }
        }
        
        $stmt = $db->prepare("INSERT INTO items (name, subtitle, description, rarity, stat_effect, quantity, image_path) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([
            $_POST['item_name'],
            $_POST['item_subtitle'],
            $_POST['item_desc'],
            $_POST['item_rarity'],
            $_POST['item_stat'],
            $_POST['item_qty'],
            $imagePath
        ]);
        $message = "Item added to inventory!";
    }
    
    if (isset($_POST['add_bounty'])) {
        // Handle bounty image upload
        $imagePath = null;
        if (!empty($_FILES['bounty_image']['tmp_name'])) {
            $uploadDir = IMAGE_PATH . '/bounties/';
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            
            $filename = uniqid() . '_' . basename($_FILES['bounty_image']['name']);
            $targetFile = $uploadDir . $filename;
            
            if (move_uploaded_file($_FILES['bounty_image']['tmp_name'], $targetFile)) {
                $imagePath = IMAGE_URL . '/bounties/' . $filename;
            }
        }
        
        $stmt = $db->prepare("INSERT INTO bounties (name, description, xp_reward, image_path) VALUES (?, ?, ?, ?)");
        $stmt->execute([
            $_POST['bounty_name'],
            $_POST['bounty_desc'],
            $_POST['bounty_xp'],
            $imagePath
        ]);
        $message = "Bounty posted!";
    }
}
?>

<?php if ($message): ?>
    <div style="background: rgba(76,175,80,0.2); border: 2px solid #4caf50; padding: 15px; margin-bottom: 20px; border-radius: 5px; text-align: center; color: #4caf50;">
        <?php echo $message; ?>
    </div>
<?php endif; ?>

<div class="grid-2">
    <div class="form-container">
        <h2 style="color: var(--gold); margin-bottom: 20px; text-align: center;">⚔️ New Quest</h2>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="add_habit" value="1">
            
            <div class="form-group">
                <label class="form-label">Quest Name</label>
                <input type="text" class="form-input" name="habit_name" required placeholder="e.g., Morning Meditation">
            </div>
            
            <div class="form-group">
                <label class="form-label">Description</label>
                <textarea class="form-textarea" name="habit_desc" rows="2" placeholder="Brief description..."></textarea>
            </div>
            
            <div class="form-group">
                <label class="form-label">Time of Day</label>
                <select class="form-select" name="habit_time">
                    <option value="morning">🌅 Morning</option>
                    <option value="midday">☀️ Midday</option>
                    <option value="evening">🌙 Evening</option>
                </select>
            </div>
            
            <div class="form-group">
                <label class="form-label">Frequency</label>
                <select class="form-select" name="habit_freq">
                    <option value="daily">Daily</option>
                    <option value="weekly">Weekly</option>
                    <option value="monthly">Monthly</option>
                    <option value="once">One-time Bounty</option>
                </select>
            </div>
            
            <div class="form-group">
                <label class="form-label">XP Reward</label>
                <input type="number" class="form-input" name="habit_xp" value="25" min="5" max="500">
            </div>
            
            <div class="form-group">
                <label class="form-label">Quest Image</label>
                <div class="file-input-wrapper">
                    <label class="file-input-label">
                        <input type="file" name="habit_image" accept="image/*" onchange="previewImage(this, 'habitPreview')">
                        Click to upload image
                    </label>
                </div>
                <img id="habitPreview" class="image-preview">
            </div>
            
            <div class="form-group">
                <label class="form-label">
                    <input type="checkbox" name="habit_calendar"> Show on Calendar
                </label>
            </div>
            
            <button type="submit" class="btn-submit">Create Quest</button>
        </form>
    </div>

    <div class="form-container">
        <h2 style="color: var(--gold); margin-bottom: 20px; text-align: center;">🎒 New Item</h2>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="add_item" value="1">
            
            <div class="form-group">
                <label class="form-label">Item Name</label>
                <input type="text" class="form-input" name="item_name" required placeholder="e.g., Potion of Focus">
            </div>
            
            <div class="form-group">
                <label class="form-label">Subtitle</label>
                <input type="text" class="form-input" name="item_subtitle" placeholder="e.g., Consumable">
            </div>
            
            <div class="form-group">
                <label class="form-label">Description</label>
                <textarea class="form-textarea" name="item_desc" rows="2"></textarea>
            </div>
            
            <div class="form-group">
                <label class="form-label">Rarity</label>
                <select class="form-select" name="item_rarity">
                    <option value="common">Common (Grey)</option>
                    <option value="uncommon">Uncommon (Green)</option>
                    <option value="rare">Rare (Blue)</option>
                    <option value="epic">Epic (Purple)</option>
                    <option value="legendary">Legendary (Orange)</option>
                </select>
            </div>
            
            <div class="form-group">
                <label class="form-label">Stat/Effect</label>
                <input type="text" class="form-input" name="item_stat" placeholder="e.g., +75 Concentration">
            </div>
            
            <div class="form-group">
                <label class="form-label">Quantity</label>
                <input type="number" class="form-input" name="item_qty" value="1" min="1">
            </div>
            
            <div class="form-group">
                <label class="form-label">Item Image</label>
                <div class="file-input-wrapper">
                    <label class="file-input-label">
                        <input type="file" name="item_image" accept="image/*" onchange="previewImage(this, 'itemPreview')">
                        Click to upload image
                    </label>
                </div>
                <img id="itemPreview" class="image-preview">
            </div>
            
            <button type="submit" class="btn-submit">Add to Inventory</button>
        </form>
    </div>
    
    <div class="form-container">
        <h2 style="color: var(--gold); margin-bottom: 20px; text-align: center;">💰 New Bounty</h2>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="add_bounty" value="1">
            
            <div class="form-group">
                <label class="form-label">Bounty Name</label>
                <input type="text" class="form-input" name="bounty_name" required placeholder="e.g., Mow the Lawn">
            </div>
            
            <div class="form-group">
                <label class="form-label">Description</label>
                <textarea class="form-textarea" name="bounty_desc" rows="2"></textarea>
            </div>
            
            <div class="form-group">
                <label class="form-label">XP Reward</label>
                <input type="number" class="form-input" name="bounty_xp" value="100" min="10" max="1000">
            </div>
            
            <div class="form-group">
                <label class="form-label">Bounty Image</label>
                <div class="file-input-wrapper">
                    <label class="file-input-label">
                        <input type="file" name="bounty_image" accept="image/*" onchange="previewImage(this, 'bountyPreview')">
                        Click to upload image
                    </label>
                </div>
                <img id="bountyPreview" class="image-preview">
            </div>
            
            <button type="submit" class="btn-submit">Post Bounty</button>
        </form>
    </div>
</div>

<?php require_once 'footer.php'; ?>


