<?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', 'department_id',
        'phone', 'telegram_chat_id', 'is_active', 'last_login'
    ];

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

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

    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    public function createdEmployees()
    {
        return $this->hasMany(Employee::class , 'created_by');
    }

    public function notificationSetting()
    {
        return $this->hasOne(NotificationSetting::class);
    }

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

    public function canManageEmployees()
    {
        return in_array($this->role, ['super_admin', 'admin_hr', 'manager']);
    }

    public function canManageAccess()
    {
        return in_array($this->role, ['super_admin', 'it_security']);
    }

    public function isManager()
    {
        return $this->role === 'manager';
    }

    public function isAdminHR()
    {
        return $this->role === 'admin_hr';
    }

    public function canApproveExtension()
    {
        return in_array($this->role, ['super_admin', 'admin_hr', 'manager', 'director']);
    }
}