<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use HasFactory, Notifiable;

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

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

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

    public function isSuperAdmin(): bool
    {
        return $this->role === 'super_admin';
    }

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

    public function avatarUrl(): string
    {
        return $this->avatar
            ? asset('storage/' . $this->avatar)
            : 'https://ui-avatars.com/api/?name=' . urlencode($this->name) . '&background=FF3D3D&color=fff';
    }

    // Хариуцсан захиалгууд
    public function assignedOrders()
    {
        return $this->hasMany(Order::class, 'assigned_to');
    }

    // Хариуцсан уулзалтууд
    public function assignedMeetings()
    {
        return $this->hasMany(Meeting::class, 'assigned_to');
    }

    // Бичсэн мэдээ
    public function articles()
    {
        return $this->hasMany(News::class, 'author_id');
    }
}
