<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'email_verified_at'
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $with = ['guru', 'siswa', 'kelas'];

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function isGuru(): bool
    {
        return $this->role === 'guru';
    }

    public function isSiswa(): bool
    {
        return $this->role === 'siswa';
    }

    public function guru()
    {
        return $this->hasOne(Guru::class)->withDefault([
            'nama' => $this->name
        ]);
    }

    public function siswa()
    {
        return $this->hasOne(Siswa::class)->withDefault([
            'nama' => $this->name
        ]);
    }

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

    public function rekapanSiswa()
    {
        return $this->hasMany(RekapanSiswa::class, 'siswa_id');
    }
}
