<?php
require_once __DIR__ . '/../src/db.php';

function log_contract_history($contract_id, $action, $details = [], $user_id = null, $username = 'System') {
    try {
        $pdo = get_db_connection();
        
        if (isset($_SESSION['user_id'])) {
            $user_id = $_SESSION['user_id'];
            $username = $_SESSION['username'];
        }

        $stmt = $pdo->prepare("
            INSERT INTO contract_history (contract_id, user_id, username, action, details)
            VALUES (?, ?, ?, ?, ?)
        ");
        $stmt->execute([$contract_id, $user_id, $username, $action, json_encode($details)]);
    } catch (Exception $e) {
        // Silenciar erro de log para não quebrar o sistema
    }
}
