<?php

// ------------------------------------------------------------
// GET TODAY'S CHAMPION
// ------------------------------------------------------------
function getTodaysChampion($pdo) {
    $today = date('Y-m-d');

    // Check if champion already selected today
    $stmt = $pdo->prepare("
        SELECT c.id, c.name, c.class_name
        FROM daily_champion dc
        JOIN champions c ON c.id = dc.champion_id
        WHERE dc.date = ?
        LIMIT 1
    ");
    $stmt->execute([$today]);
    $champion = $stmt->fetch();

    if ($champion) {
        return $champion;
    }

    // Otherwise pick a random champion
    $pick = $pdo->query("
        SELECT id, name, class_name
        FROM champions
        ORDER BY RAND()
        LIMIT 1
    ")->fetch();

    // Store it
    $insert = $pdo->prepare("
        INSERT INTO daily_champion (date, champion_id)
        VALUES (?, ?)
    ");
    $insert->execute([$today, $pick['id']]);

    return $pick;
}


// ------------------------------------------------------------
// GET HABITS FOR TODAY
// ------------------------------------------------------------
function getHabitsForToday($pdo) {
    $today = date('Y-m-d');

    $stmt = $pdo->prepare("
        SELECT *
        FROM habits
        WHERE 
            (frequency = 'daily')
            OR (frequency = 'weekly' AND WEEKDAY(?) = WEEKDAY(created_at))
            OR (frequency = 'fortnightly' AND MOD(WEEK(?), 2) = MOD(WEEK(created_at), 2))
            OR (frequency = 'monthly' AND DAY(?) = DAY(created_at))
            OR (frequency = 'one_off' AND specific_date = ?)
        ORDER BY created_at DESC
    ");

    $stmt->execute([$today, $today, $today, $today]);
    return $stmt->fetchAll();
}


// ------------------------------------------------------------
// LEVELING SYSTEM
// ------------------------------------------------------------
function getXPForNextLevel($level) {
    return $level * 100;
}

function addXP($pdo, $championId, $xp) {
    // Get current stats
    $stmt = $pdo->prepare("
        SELECT total_xp, level
        FROM champion_stats
        WHERE champion_id = ?
    ");
    $stmt->execute([$championId]);
    $stats = $stmt->fetch();

    $totalXP = $stats['total_xp'] + $xp;
    $level   = $stats['level'];

    // Level up?
    $xpNeeded = getXPForNextLevel($level);
    while ($totalXP >= $xpNeeded) {
        $totalXP -= $xpNeeded;
        $level++;
        $xpNeeded = getXPForNextLevel($level);
    }

    // Save
    $update = $pdo->prepare("
        UPDATE champion_stats
        SET total_xp = ?, level = ?
        WHERE champion_id = ?
    ");
    $update->execute([$totalXP, $level, $championId]);
}


// ------------------------------------------------------------
// LOW STOCK CHECK
// ------------------------------------------------------------
function isLowStock($item) {
    $qty = $item['quantity'];
    $max = $item['quantity_max'];

    if ($qty <= $GLOBALS['LOW_STOCK_ABSOLUTE']) {
        return true;
    }

    if ($max > 0) {
        $percent = ($qty / $max) * 100;
        if ($percent <= $GLOBALS['LOW_STOCK_PERCENT']) {
            return true;
        }
    }

    return false;
}


// ------------------------------------------------------------
// SAFE FETCH HELPERS
// ------------------------------------------------------------
function fetchAllItems($pdo) {
    return $pdo->query("SELECT * FROM items ORDER BY id DESC")->fetchAll();
}

function fetchItem($pdo, $id) {
    $stmt = $pdo->prepare("SELECT * FROM items WHERE id = ?");
    $stmt->execute([$id]);
    return $stmt->fetch();
}

function fetchHabit($pdo, $id) {
    $stmt = $pdo->prepare("SELECT * FROM habits WHERE id = ?");
    $stmt->execute([$id]);
    return $stmt->fetch();
}

?>