<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class StudentExamSession extends Model
{
    protected $table = 'student_exam_sessions';

    protected $guarded = [];

    protected function casts(): array
    {
        return [
            'draft_answers' => 'array',
            'metadata' => 'array',
            'duration_minutes' => 'integer',
            'elapsed_seconds' => 'integer',
            'warning_count' => 'integer',
            'suspicious_event_count' => 'integer',
            'started_at' => 'datetime',
            'last_heartbeat_at' => 'datetime',
            'submitted_at' => 'datetime',
            'auto_submitted_at' => 'datetime',
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
        ];
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function assignment(): BelongsTo
    {
        return $this->belongsTo(CompetitionAssignment::class, 'competition_assignment_id');
    }

    public function contest(): BelongsTo
    {
        return $this->belongsTo(MatchContest::class, 'competition_id');
    }

    public function questionSet(): BelongsTo
    {
        return $this->belongsTo(QuestionSetup::class, 'question_set_id');
    }

    public function deviceSession(): BelongsTo
    {
        return $this->belongsTo(StudentDeviceSession::class, 'student_device_session_id');
    }

    public function accessToken(): BelongsTo
    {
        return $this->belongsTo(MobileAccessToken::class, 'mobile_access_token_id');
    }

    public function activityLogs(): HasMany
    {
        return $this->hasMany(StudentActivityLog::class, 'student_exam_session_id');
    }
}
