<?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; // Jika pakai Sanctum, jika tidak bisa dihapus

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'username',
        'nip',
        'email',
        'password',
        'role',
        'avatar'
        // 'subject_id',  <-- HAPUS INI. Sudah tidak dipakai karena diganti tabel pivot
    ];

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

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

    /**
     * RELASI BARU (MANY TO MANY)
     * Menggunakan nama 'subjects' (Jamak) agar sesuai dengan Controller.
     * Menggunakan belongsToMany agar 1 guru bisa punya banyak mapel.
     */
    public function subjects()
    {
        // Parameter ke-2 'subject_user' adalah nama tabel pivot/perantara
    return $this->belongsToMany(Subject::class, 'teacher_subject', 'user_id', 'subject_id');    }
    
    // Helper untuk cek role
    public function isSuperAdmin() { return $this->role === 'super_admin'; }
    public function isAdminMapel() { return $this->role === 'admin_mapel'; }
    public function isGuru() { return $this->role === 'guru_mapel'; }
}