<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements FilamentUser
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use \BezhanSalleh\FilamentShield\Traits\HasPanelShield, HasFactory, HasRoles, Notifiable;

    public function canAccessPanel(\Filament\Panel $panel): bool
    {
        if ($panel->getId() === 'student') {
            return $this->hasRole('student');
        }

        // For Admin panel, fall back to Shield logic (which checks roles/permissions)
        // Or strictly check panel_user/super_admin
        return $this->hasRole(['super_admin', 'panel_user']) || $this->hasPermissionTo('view_admin');
    }

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

    public function courses()
    {
        return $this->belongsToMany(Course::class, 'course_students')
            ->withPivot(['progress', 'is_completed', 'completed_at', 'enrolled_at'])
            ->withTimestamps();
    }

    public function examAttempts()
    {
        return $this->hasMany(ExamAttempt::class);
    }

    public function certificates()
    {
        return $this->hasMany(Certificate::class);
    }

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

    /**
     * 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',
        ];
    }
}
