<?php

namespace App\Models;

use App\Events\UserRegistered;
use App\Mail\PasswordResetRequested;
use App\Support\HasRolesUuid;
use App\Support\HasSocialLogin;
use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable {
    use Notifiable, UuidScopeTrait, HasFactory, HasApiTokens, HasRoles, SoftDeletes, HasSocialLogin, RelationValuesTrait, HasRolesUuid {
        HasRolesUuid::getStoredRole insteadof HasRoles;
    }

    protected $dates = [
        'deleted_at'
    ];

    protected $fillable = [
        'uuid',
        'asset_id',
        'name',
        'phone',
        'email',
        'password'
    ];

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



    public function socialProviders(): HasMany {
        return $this->hasMany(SocialProvider::class);
    }

	public function avatar(): BelongsTo {
    	return $this->belongsTo(Asset::class, 'asset_id');
	}


    public function getInitialsAttribute(): string {
        return collect(explode(' ', $this->name))->slice(0, 2)->map(function($item) {
            return Str::upper(Str::substr($item, 0, 1));
        })->join('');
    }

    public function getFirstAndMidNameAttribute(): string {
        return collect(explode(' ', $this->name))->slice(1, 2)->join(' ');
    }

    public function getIsAdminAttribute(): bool {
        return $this->hasRole('Administrator');
    }

    public function getIsPrivilegedAttribute() {
        return $this->isAdmin;
    }


    public static function getByData($data, $triggerEvent = false) {
        $result = false;
        if ($email = trim($data['email'] ?? null)) {
            $result = self::query()->where(['email' => $email])->first();
            if (!$result) {
                $password = $data['password'] ?? (App::environment('local') ? 'Qwerty1!' : Str::random(8));
                $result = self::create(['email' => $email, 'password' => $password]);
                $result->update(['name' => trim($data['name'] ?? null), 'phone' => trim($data['phone'] ?? null)]);
                $result->assignRole($data['role'] ?? 'User');
                if ($triggerEvent) event(new UserRegistered($result, $password));
            } else {
                if ($val = trim($data['phone'] ?? null)) $result->update(['phone' => $val]);
            }
            if ($val = $data['avatar'] ?? null) $result->setAvatar($val);
        }
        return $result;
    }

    public function setAvatar($val) {
        $asset = Asset::byUuid($val)->first();
        $this->update(['asset_id' => $asset->id ?? null]);
    }



    public static function create(array $attributes = []) {
        if (array_key_exists('password', $attributes)) {
            $attributes['password'] = Hash::make($attributes['password']);
        }
        return static::query()->create($attributes);
    }


    public function sendPasswordResetNotification($token) {
        Mail::to($this->email)->send(new PasswordResetRequested($this, $token));
    }

}
