<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StudentRank extends Model
{
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'student_ranks';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'usr_id',
        'class_id', 
        'session',
        'sequence',
        'total_points',
        'total_bareme',
        'average',
        'rank',
        'appreciation',
        'lang_type_rank'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'total_points' => 'decimal:2',
        'total_bareme' => 'decimal:2',
        'average' => 'decimal:2',
        'sequence' => 'integer',
        'rank' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    /**
     * Get the student that owns the rank.
     */
    public function student()
    {
        return $this->belongsTo(User::class, 'usr_id', 'usr_id');
    }

    /**
     * Get the class that owns the rank.
     */
    public function classSchool()
    {
        return $this->belongsTo(ClassSchool::class, 'class_id', 'class_id');
    }

    /**
     * Scope a query to filter by session and sequence.
     */
    public function scopeForPeriod($query, $session, $sequence)
    {
        return $query->where('session', $session)
                    ->where('sequence', $sequence);
    }

    /**
     * Scope a query to filter by class.
     */
    public function scopeForClass($query, $classId)
    {
        return $query->where('class_id', $classId);
    }

    /**
     * Scope a query to filter by student.
     */
    public function scopeForStudent($query, $studentId)
    {
        return $query->where('usr_id', $studentId);
    }

    /**
     * Get formatted total points (ex: "450/600")
     */
    public function getFormattedTotalAttribute()
    {
        return $this->total_points . '/' . $this->total_bareme;
    }

    /**
     * Get formatted average (ex: "15.00/20")
     */
    public function getFormattedAverageAttribute()
    {
        return number_format($this->average, 2) . '/20';
    }

    /**
     * Get rank with suffix (ex: "1st", "2nd", "3rd")
     */
    public fun