<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name',
        'username',
        'email',
        'phone',
        'designation',
        'photo',
        'access_code',
        'access_code_plain',
        'role',
        'is_active',
        'password',
        'password_plain',
    ];

    protected $hidden = [
        'password',
        'password_plain',
        'remember_token',
        'access_code',
        'access_code_plain',
    ];

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

    public function getInitialsAttribute(): string
    {
        $words = explode(' ', $this->name);
        $initials = '';
        foreach (array_slice($words, 0, 2) as $word) {
            $initials .= strtoupper($word[0] ?? '');
        }
        return $initials;
    }

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