<?php
// includes/activity_log.php
// Wires up tbl_activity_log, which existed since an early migration but
// had nothing writing to it. Call this after any meaningful, successful
// state change on a job — creation, edit, approval, unlock. Deliberately
// NOT called for every minor action (e.g. individual photo uploads) to
// keep the log meaningful rather than noisy.

function logActivity(PDO $pdo, int $jobId, string $action, string $details = ''): void
{
    try {
        $me = function_exists('current_user') ? current_user() : null;
        $stmt = $pdo->prepare(
            "INSERT INTO tbl_activity_log (JobID, UserID, Action, Details) VALUES (?, ?, ?, ?)"
        );
        $stmt->execute([$jobId, $me['id'] ?? null, $action, $details ?: null]);
    } catch (\PDOException $e) {
        // Never let audit logging break the actual operation it's logging.
        error_log('logActivity failed: ' . $e->getMessage());
    }
}
