<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Mark extends Model
{
    protected $fillable = ['student_id', 'exam_id', 'subject_id', 'score', 'out_of', 'grade', 'points', 'remarks', 'entered_by'];
    protected $casts = ['score' => 'decimal:2', 'out_of' => 'decimal:2'];

    public function student() { return $this->belongsTo(Student::class); }
    public function exam() { return $this->belongsTo(Exam::class); }
    public function subject() { return $this->belongsTo(Subject::class); }
    public function enteredBy() { return $this->belongsTo(User::class, 'entered_by'); }

    public static function computeGrade(float $percentage): array
    {
        return match(true) {
            $percentage >= 80 => ['grade' => 'A', 'points' => 12, 'remarks' => 'Excellent'],
            $percentage >= 75 => ['grade' => 'A-', 'points' => 11, 'remarks' => 'Excellent'],
            $percentage >= 70 => ['grade' => 'B+', 'points' => 10, 'remarks' => 'Very Good'],
            $percentage >= 65 => ['grade' => 'B', 'points' => 9, 'remarks' => 'Good'],
            $percentage >= 60 => ['grade' => 'B-', 'points' => 8, 'remarks' => 'Good'],
            $percentage >= 55 => ['grade' => 'C+', 'points' => 7, 'remarks' => 'Average'],
            $percentage >= 50 => ['grade' => 'C', 'points' => 6, 'remarks' => 'Average'],
            $percentage >= 45 => ['grade' => 'C-', 'points' => 5, 'remarks' => 'Below Average'],
            $percentage >= 40 => ['grade' => 'D+', 'points' => 4, 'remarks' => 'Below Average'],
            $percentage >= 35 => ['grade' => 'D', 'points' => 3, 'remarks' => 'Poor'],
            $percentage >= 30 => ['grade' => 'D-', 'points' => 2, 'remarks' => 'Poor'],
            default           => ['grade' => 'E', 'points' => 1, 'remarks' => 'Fail'],
        };
    }
}
