<?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;

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

    protected $fillable = ['name', 'email', 'password', 'role', 'avatar', 'bio', 'is_active', 'last_login_at'];
    protected $hidden   = ['password', 'remember_token'];
    protected $casts    = [
        'email_verified_at' => 'datetime',
        'last_login_at'     => 'datetime',
        'is_active'         => 'boolean',
        'password'          => 'hashed',
    ];

    public function posts()         { return $this->hasMany(Post::class, 'author_id'); }
    public function comments()      { return $this->hasMany(Comment::class); }
    public function bookmarks()     { return $this->hasMany(Bookmark::class); }
    public function apiKeys()       { return $this->hasMany(ApiKey::class); }
    public function notifications() { return $this->hasMany(Notification::class); }

    public function isAdmin(): bool       { return in_array($this->role, ['admin', 'super_admin']); }
    public function isSuperAdmin(): bool  { return $this->role === 'super_admin'; }
    public function isEditor(): bool      { return in_array($this->role, ['editor', 'admin', 'super_admin']); }
}
