<?php

namespace App\Services;

use App\Models\StudentActivityLog;
use App\Models\StudentDeviceSession;
use App\Models\StudentExamSession;
use Illuminate\Http\Request;

class StudentActivityLogger
{
    public function log(
        int $userId,
        string $eventType,
        array $payload = [],
        string $level = 'info',
        ?Request $request = null,
        ?StudentDeviceSession $deviceSession = null,
        ?StudentExamSession $examSession = null,
        string $source = 'app',
    ): StudentActivityLog {
        return StudentActivityLog::query()->create([
            'user_id' => $userId,
            'student_device_session_id' => $deviceSession?->id,
            'student_exam_session_id' => $examSession?->id,
            'event_type' => $eventType,
            'level' => $level,
            'source' => $source,
            'ip_address' => $request?->ip(),
            'user_agent' => $request?->userAgent(),
            'payload' => $payload !== [] ? $payload : null,
            'occurred_at' => now(),
        ]);
    }
}
