<?php
// admin/include/activity_logger.php
class ActivityLogger {
    public function __construct(private PDO $db) {}

    public function log(
        int $submissionId,
        string $type,
        string $description,
        array $metadata = [],
        ?int $createdBy = null
    ): void {
        try {
            $this->db->exec("CREATE TABLE IF NOT EXISTS submission_activity_log (
                id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                submission_id INT UNSIGNED NOT NULL,
                activity_type VARCHAR(60) NOT NULL,
                activity_description TEXT NOT NULL,
                metadata JSON NULL,
                created_by INT UNSIGNED NULL,
                created_at DATETIME DEFAULT NOW(),
                KEY idx_submission (submission_id)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

            $this->db->prepare("
                INSERT INTO submission_activity_log
                (submission_id, activity_type, activity_description, metadata, created_by)
                VALUES (?, ?, ?, ?, ?)
            ")->execute([
                $submissionId,
                $type,
                $description,
                $metadata ? json_encode($metadata) : null,
                $createdBy,
            ]);
        } catch(PDOException $e) {
            error_log('ActivityLogger error: ' . $e->getMessage());
        }
    }
}