<?php
// includes/logger.php - Versão Completa com Custos

class Logger {

    // 1. Log de Login
    public static function login($pdo, $user_id, $status, $ip) {
        try {
            $sql = "INSERT INTO login_logs (type, user_id, status, login_ip, date) VALUES (?, ?, ?, ?, ?)";
            $pdo->prepare($sql)->execute(['reseller', $user_id, $status, $ip, time()]);
        } catch (Exception $e) {}
    }

    // 2. Log de Créditos (Transferências)
    public static function credit($pdo, $admin_id, $target_id, $amount, $reason) {
        try {
            $sql = "INSERT INTO credits_log (admin_id, target_id, amount, date, reason) VALUES (?, ?, ?, ?, ?)";
            $pdo->prepare($sql)->execute([$admin_id, $target_id, $amount, time(), $reason]);
        } catch (Exception $e) {}
    }

    // 3. Log de Ações do Usuário (CRIAR, EDITAR, RENOVAR) - ATUALIZADO
    public static function userAction($pdo, $owner_id, $action, $description, $cost = 0) {
        try {
            // Primeiro: Descobre quanto de crédito o revendedor tem AGORA (pós-gasto)
            $stmt = $pdo->prepare("SELECT credits FROM revendedores WHERE id = ?");
            $stmt->execute([$owner_id]);
            $credits_after = $stmt->fetchColumn();
            if($credits_after === false) $credits_after = 0;

            // Grava o log completo com custo e saldo restante
            $sql = "INSERT INTO users_logs (owner, action, deleted_info, cost, credits_after, date) VALUES (?, ?, ?, ?, ?, ?)";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$owner_id, $action, $description, $cost, $credits_after, time()]);
        } catch (Exception $e) {}
    }
}
?>
