<?php

namespace App\Models\UserManagement;

use App\Models\LocalizationManagement\Bootables\Addressable;
use App\Models\SecurityManagement\Role;
use App\Models\Traits\Bootables\Auditable;
use App\Models\Traits\CanUploadFiles;
use App\Models\Traits\Activable;
use Axn\EloquentAuthorable\AuthorableTrait;
use Database\Factories\CustomerFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Nicolaslopezj\Searchable\SearchableTrait;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Permission\Traits\HasRoles as SpatieHasRoles;
use Spatie\MediaLibrary\HasMedia as SpatieHasMedia;

class User extends Authenticatable implements MustVerifyEmail,SpatieHasMedia{
    use HasFactory, Notifiable, searchableTrait,SoftDeletes,AuthorableTrait,
        Activable,Addressable,Auditable,
        CanUploadFiles,InteractsWithMedia,
        SpatieHasRoles {permissions as public autorizations; }

    protected $guarded = ["created_at"];
    const FK_ID = "user_id";

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

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $appends = [
        'full_name',
        "has_files",
        "avatar",
        "status_label",
        "privileges",

        "roles_ids",
        "full_default_address",
        "balance",

    ];

    protected $searchable = [
        'columns' => [
            'users.name' => 10,
            'users.username' => 10,
            'users.email' => 10,
            'users.phone' => 10,
        ],
    ];


    public function getRouteKeyName(){
        return "id";
    }

    protected static function newFactory(){
        return CustomerFactory::new();
    }
//    public function guardName(){
//        return "web";
//    }

    public function getFullNameAttribute(){
        return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
    }

    public function getPrivilegesAttribute(){
        return $this->getAllPermissions()->pluck("name")->toArray();
    }

    public function getRolesIdsAttribute(){
        return $this->roles->pluck('id')->toArray();
    }

    public function getFullDefaultAddressAttribute(){
        return "---";
    }
    public function getBalanceAttribute(){
        return 0;
    }

    public function isAdminOrSupervisor(){
        return $this->hasRole('admin')
               || $this->hasRole('supervisor')
               || $this->hasRole(Role::SUPER_ADMIN);
    }

    public function scopeCustomers($query){
        return $query->where(UserType::FK_ID,UserType::CUSTOMER);
    }
    public function scopeStaffs($query){
        return $query->where(UserType::FK_ID,UserType::ADMIN);
    }


    public function isCustomer(){
        return $this->guardName() == "customer";
    }

    public function isAdmin(){
        return $this->guardName() == "admin";
    }



}
