<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name', 'name_si', 'email', 'password',
        'phone', 'photo', 'role',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    // Role helpers
    public function isAdmin(): bool    { return $this->role === 'admin'; }
    public function isTeacher(): bool  { return $this->role === 'teacher'; }
    public function isStudent(): bool  { return $this->role === 'student'; }

    // Relationships
    public function studentProfile() { return $this->hasOne(StudentProfile::class); }
    public function teacherProfile() { return $this->hasOne(TeacherProfile::class); }

    public function enrolledSubjects()
    {
        return $this->belongsToMany(Subject::class, 'student_subject', 'user_id', 'subject_id')
                    ->withPivot('enrolled_at');
    }

    public function taughtSubject()    { return $this->hasOne(Subject::class, 'teacher_id'); }
    public function lessons()          { return $this->hasMany(Lesson::class, 'teacher_id'); }
    public function quizzes()          { return $this->hasMany(Quiz::class, 'teacher_id'); }
    public function marks()            { return $this->hasMany(Mark::class, 'student_id'); }
    public function announcements()    { return $this->hasMany(Announcement::class, 'author_id'); }

    public function photoUrl(): string
    {
        return $this->photo
            ? asset('images/' . $this->photo)
            : asset('images/default-avatar.png');
    }
}
