<?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;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    protected $fillable = [
        'name', 'email', 'phone', 'avatar', 'password',
        'two_fa_secret', 'two_fa_enabled', 'two_fa_code', 'two_fa_expires_at',
        'is_active', 'last_login_at', 'last_login_ip', 'school_id',
    ];

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

    protected $casts = [
        'email_verified_at' => 'datetime',
        'two_fa_expires_at' => 'datetime',
        'last_login_at' => 'datetime',
        'two_fa_enabled' => 'boolean',
        'is_active' => 'boolean',
        'password' => 'hashed',
    ];

    public function school()
    {
        return $this->belongsTo(School::class);
    }

    public function student()
    {
        return $this->hasOne(Student::class);
    }

    public function teacher()
    {
        return $this->hasOne(Teacher::class);
    }

    public function parentProfile()
    {
        return $this->hasOne(ParentProfile::class, 'user_id');
    }

    public function activityLogs()
    {
        return $this->hasMany(ActivityLog::class);
    }
}
