<?php

namespace App\Models;

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

class Grade extends Model
{
    protected $fillable = [
        'student_id', 'school_class_id', 'subject_id', 'teacher_id',
        'exam_type', 'score', 'status', 'remarks', 'graded_by', 'graded_at'
    ];

    protected $casts = [
        'graded_at' => 'datetime',
        'score' => 'integer',
    ];

    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class);
    }

    public function schoolClass(): BelongsTo
    {
        return $this->belongsTo(SchoolClass::class);
    }

    public function subject(): BelongsTo
    {
        return $this->belongsTo(Subject::class);
    }

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

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

    public function getGradeLetterAttribute(): string
    {
        $score = $this->score;
        
        if ($score >= 80) return '4';
        if ($score >= 70) return '3';
        if ($score >= 60) return '3';
        if ($score >= 50) return '2';
        if ($score >= 40) return '1';
        return '0';
    }

    public function getRemarkAttribute(): string
    {
        $score = $this->score;
        
        if ($score >= 80) return 'Exceeds Expectation';
        if ($score >= 70) return 'Meets Expectation';
        if ($score >= 60) return 'Approaches Expectation';
        if ($score >= 50) return 'Below Expectation';
        return 'Below Expectation';
    }
}