<?php

namespace App\Models;

use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

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

    protected $fillable = [
        'name',
        'email',
        'phone',
        'password',
        'role',
        'is_active',
    ];

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

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

    // ── Relations ─────────────────────────────────────────────────
    public function leads()
    {
        return $this->hasMany(\App\Models\Lead::class, 'assigned_to');
    }

    public function blogArticles()
    {
        return $this->hasMany(\App\Models\BlogArticle::class, 'author_id')->latest();
    }

    public function notifications()
    {
        return $this->hasMany(\App\Models\UserNotification::class)->latest();
    }

    // ── Role helpers ──────────────────────────────────────────────
    public function isOwner(): bool    { return $this->role === 'owner'; }
    public function isAdmin(): bool    { return in_array($this->role, ['owner', 'admin']); }
    public function isEmployee(): bool { return $this->role === 'employee'; }

    public function hasRole(string|array $roles): bool
    {
        return in_array($this->role, (array) $roles);
    }
}
