<?php

namespace App\Models;

use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Str;

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

    public $incrementing = false;
    protected $keyType = 'string';
    protected $primaryKey = 'uuid';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'parent_uuid',
        'phone_number',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'phone_number_verified_at' => 'datetime',
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    const Calendars = [
        'Gregorian',
        'Jalali',
        'Hijri',
    ];

    public static function boot(): void
    {
        parent::boot();
        static::creating(function ($model) {
            if (empty($model->uuid))
                $model->uuid = (string) Str::uuid();
        });
    }

    public function Profile(): HasOne
    {
        return $this->hasOne(Profile::class, 'user_uuid', 'uuid');
    }

    public function Projects(): HasMany
    {
        return $this->hasMany(Project::class, 'user_uuid', 'uuid');
    }

    public function Parent(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_uuid', 'uuid');
    }

    public function Children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_uuid', 'uuid');
    }

    public function Roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'role_user');
    }

    public function FullName(): string
    {
        $profile = $this->Profile;
        return translate($profile->first_name) . ' ' . translate($profile->last_name);
    }

    public function HasRole($role): bool
    {
        return in_array($role, $this->Roles->pluck('name')->toArray());
    }

    public function HasPermission($permission): bool
    {
        $permissions = collect();
        foreach ($this->Roles as $role) {
            $permissions->push($role->Permissions);
        }
        return in_array($permission, $permissions->pluck('name')->toArray());
    }
}
