<?php

namespace App\Models;

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

class Prestasi extends Model
{
    use HasFactory;

    protected $fillable = [
        'admin_id',
        'jurusan_id',
        'nama_siswa',
        'nama_prestasi',
        'peringkat',
        'tahun_prestasi',
        'foto_prestasi',
        'status',
        'urutan'
    ];

    protected $casts = [
        'status' => 'boolean',
        'tahun_prestasi' => 'integer',
        'urutan' => 'integer'
    ];

    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    public function jurusan()
    {
        return $this->belongsTo(Jurusan::class, 'jurusan_id');
    }

    public function getFotoPrestasiUrlAttribute()
    {
        if ($this->foto_prestasi) {
            return asset('storage/prestasi/' . $this->foto_prestasi);
        }
        return asset('images/default-img.png');
    }

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

    public function scopeOrdered($query)
    {
        return $query->orderBy('urutan', 'desc')->orderBy('created_at', 'desc');
    }
}
