<?php

namespace App\Models;

use App\Models\School;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name',
        'email',
        'phone',
        'password',
        'role',
        'login_id', // Add this field for student/staff ID
    ];

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

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    // Role check methods
    public function isSuperAdmin(): bool
    {
        return $this->role === 'super_admin';
    }

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

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

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

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

    // Relationship with School
    public function school()
    {
        return $this->belongsTo(School::class);
    }
        // Teacher-specific relationships
    public function taughtClasses()
    {
        return $this->belongsToMany(SchoolClass::class, 'school_class_subject_teacher', 'teacher_id', 'school_class_id')
                    ->withPivot('subject_id');
    }

    public function taughtSubjects()
    {
        return $this->belongsToMany(Subject::class, 'school_class_subject_teacher', 'teacher_id', 'subject_id')
                    ->withPivot('school_class_id');
    }

    public function assignments()
    {
        return $this->hasMany(Assignment::class, 'teacher_id');
    }

    public function staffProfile()
    {
        return $this->hasOne(StaffProfile::class);
    }

    public function children()
    {
        return $this->hasMany(Student::class, 'parent_id');
    }
}