<?php

namespace App\Models;

use App\Models\BaseModel;

class FinalScore extends BaseModel
{
    protected $table = 'final_score';

    // CI model didn't specify timestamps behavior; table includes created_at/updated_at.
    // If you want Laravel to auto-manage them, keep true (recommended).
    public $timestamps = true;

    protected $fillable = [
        'student_id',
        'school_id',
        'class_section_id',
        'updated_by',
        'score',
        'score_letter',
        'comment',
        'school_year',
        'created_at',
        'updated_at',
        'semester',
    ];

    protected $casts = [
        'student_id'       => 'integer',
        'school_id'        => 'integer',
        'class_section_id' => 'integer',
        'teacher_id'       => 'integer',
        'score'            => 'decimal:2', // change to 'integer' if score is whole number
    ];

    /* Optional relationships */
    public function student()
    {
        return $this->belongsTo(Student::class, 'student_id');
    }

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

    public function classSection()
    {
        return $this->belongsTo(ClassSection::class, 'class_section_id');
    }

    /**
     * Equivalent of CI getFinalExamScore()
     */
    public static function getFinalExamScore(int $studentId, string $semester, string $schoolYear)
    {
        $row = static::query()
            ->select('score')
            ->where('student_id', $studentId)
            ->where('semester', $semester)
            ->where('school_year', $schoolYear)
            ->first();

        return $row?->score;
    }
}