<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable = ['username', 'password', 'role_id', 'id_siswa'];
    protected $hidden = ['password'];

    public function role()
    {
        return $this->belongsTo(Role::class, 'role_id', 'id_role');
    }

    public function siswa()
    {
        return $this->belongsTo(SiswaMagang::class, 'id_siswa');
    }

    public function attendance()
    {
        return $this->hasMany(Attendance::class, 'user_id');
    }

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

    public function isSiswaMagang()
    {
        return $this->role->nama_role === 'siswa magang';
    }

    public function pembimbingMagang()
{
    return $this->belongsTo(PembimbingMagang::class, 'id_pembimbing_magang');
}

public function pembimbingSekolah()
{
    return $this->belongsTo(PembimbingSekolah::class, 'id_pembimbing_sekolah');
}

public function jurnal()
{
    return $this->hasMany(Jurnal::class);
}

public function isPembimbingMagang()
{
    return $this->role_id == 3; // Assuming role_id 3 for pembimbing magang
}

public function isPembimbingSekolah()
{
    return $this->role_id == 4; // Assuming role_id 4 for pembimbing sekolah
}

    public function todayAttendance()
    {
        return $this->attendance()->whereDate('date', today())->first();
    }

    public function getNameAttribute()
    {
        return $this->siswa ? $this->siswa->nama_siswa : $this->username;
    }
}