<?php

namespace App\Models;

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

class Siswa extends Model
{
    use HasFactory;

    protected $table = 'siswa';

    protected $fillable = [
        'user_id',
        'kelas_id',
        'nis',
        'nisn',
        'nama',
        'jenis_kelamin',
        'no_hp',
        'foto',
        'alamat',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function kelas()
    {
        return $this->belongsTo(Kelas::class);
    }

    public function pengerjaanUjian()
    {
        return $this->hasMany(PengerjaanUjian::class);
    }

    public function nilaiUjian()
    {
        return $this->hasMany(NilaiUjian::class);
    }

    public function getFotoUrlAttribute(): ?string
    {
        if (!$this->foto) return null;
        return asset('uploads/siswa/' . $this->foto);
    }
}
