<?php

namespace App\Models\SelfPaced;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * ExamResult Model
 *
 * The auto-graded outcome of an ExamAttempt.
 * One result per attempt (unique constraint on attempt_id).
 *
 * @property int $id
 * @property int $attempt_id
 * @property int $enrollment_id
 * @property int $user_id
 * @property int $course_id
 * @property int $total_questions
 * @property int $correct_answers
 * @property float $score_percentage
 * @property bool $passed
 * @property int $passing_threshold
 * @property string|null $result_slip_path
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 */
class ExamResult extends Model
{
    protected $table = 'self_paced_exam_results';

    protected $fillable = [
        'attempt_id',
        'enrollment_id',
        'user_id',
        'course_id',
        'total_questions',
        'correct_answers',
        'score_percentage',
        'passed',
        'passing_threshold',
        'result_slip_path',
        'topic_scores',
    ];

    protected $casts = [
        'total_questions'  => 'integer',
        'correct_answers'  => 'integer',
        'score_percentage' => 'decimal:2',
        'passed'           => 'boolean',
        'passing_threshold' => 'integer',
        'topic_scores'     => 'array',
        'created_at'       => 'datetime',
        'updated_at'       => 'datetime',
    ];

    protected $attributes = [
        'passed' => false,
    ];

    // =========================================================================
    // RELATIONSHIPS
    // =========================================================================

    public function attempt(): BelongsTo
    {
        return $this->belongsTo(ExamAttempt::class, 'attempt_id');
    }

    public function enrollment(): BelongsTo
    {
        return $this->belongsTo(Enrollment::class, 'enrollment_id');
    }

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

    public function course(): BelongsTo
    {
        return $this->belongsTo(Course::class, 'course_id');
    }

    public function certificate(): HasOne
    {
        return $this->hasOne(SelfPacedCertificate::class, 'result_id');
    }

    // =========================================================================
    // QUERY SCOPES
    // =========================================================================

    public function scopePassed($query)
    {
        return $query->where('passed', true);
    }

    public function scopeFailed($query)
    {
        return $query->where('passed', false);
    }

    public function scopeForCourse($query, int $courseId)
    {
        return $query->where('course_id', $courseId);
    }

    // =========================================================================
    // HELPER METHODS
    // =========================================================================

    public function getScoreLabel(): string
    {
        return $this->correct_answers . ' / ' . $this->total_questions
            . ' (' . number_format((float) $this->score_percentage, 1) . '%)';
    }

    public function getStatusBadgeColor(): string
    {
        return $this->passed ? 'green' : 'red';
    }

    public function getStatusText(): string
    {
        return $this->passed ? 'PASS' : 'FAIL';
    }

    public function hasResultSlip(): bool
    {
        return !empty($this->result_slip_path);
    }
}
