<?php

namespace App\Models;

use App\Events\UserRegistered;
use App\Mail\PasswordResetRequested;
use App\Models\Advisories\Advisory;
use App\Models\Advisories\AdvisoryMember;
use App\Models\Advisories\AdvisoryMemberRank;
use App\Models\Applications\Conclusion;
use App\Models\Companies\Company;
use App\Models\Companies\CompanyMember;
use App\Models\Companies\CompanyMemberRole;
use App\Models\Objects\Field;
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\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
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 companies(): BelongsToMany {
        return $this->belongsToMany(Company::class, 'company_members');
    }

    public function membership(): HasMany {
        return $this->hasMany(CompanyMember::class);
    }
    public function advisoryMembership(): HasManyThrough {
        return $this->hasManyThrough(AdvisoryMember::class, CompanyMember::class);
    }


    public function applicationConclusions(): HasMany {
        return $this->hasMany(Conclusion::class, 'author_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') || $this->isMainCompanyAdmin;
    }
    public function getIsModeratorAttribute(): bool {
        return $this->membership()->where(['role' => CompanyMemberRole::MODERATOR])->mainCompany()->exists();
    }
    public function getIsMainCompanyAdminAttribute(): bool {
        return $this->membership()->where(['role' => CompanyMemberRole::ADMINISTRATOR])->mainCompany()->exists();
    }
    public function getIsMainCompanyMemberAttribute(): bool {
        return $this->companies()->where(['is_main' => 1])->exists();
    }
    public function getIsExpertAttribute(): bool {
        return $this->membership()->mainCompany()->whereHas('objects', function($query) {
            Field::applyFilters($query, collect(['types' => 'company-member-properties', 'moderate-permissions' => 'applications']));
        })->exists();
    }

    public function getPrivilegesAttribute(): array {
        return [
            'admin' => $this->isAdmin,
            'expert' => $this->isExpert,
            'main_company_member' => $this->isMainCompanyMember
        ];
    }




    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 setPassword(string $password): bool {
        return $this->update(['password' => Hash::make($password)]);
    }

    public function checkPassword(string $password): bool {
        return Hash::check($password, $this->password);
    }

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

}
