<?php

namespace LaraBB\User\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use App\Traits\Uuid;
use Illuminate\Support\Collection;
use LaraBB\Group\Models\Group;
use LaraBB\Invite\Models\Invite;
use LaraBB\Profile\Models\Profile;

/**
 * Class User
 *
 * @property int $id
 * @property string $uuid
 * @property string $pid
 * @property Carbon $created_at
 * @property string $created_uuid
 * @property Carbon $updated_at
 * @property string $updated_uuid
 * @property Carbon $deactivated_at
 * @property string $deactivated_uuid
 * @property Carbon $registered_at
 * @property string $registered_ip
 * @property Carbon $lastactive_at
 * @property string $lastactive_ip
 * @property string $username
 * @property string $email_hashed
 * @property string $email_encrypted
 * @property string $password
 * @property string|null $remember_token
 * @property integer $passwordchange_required
 * @property Profile $profile
 * @property EloquentCollection $groups
 * @property EloquentCollection $invites
 *
 * @package LaraBB\User\Models
 */
class User extends Authenticatable
{
    use Uuid;
    use Notifiable;

    /**
     * @var string
     */
    protected $table = 'users';
    /**
     * @var string
     */
    protected $primaryKey = 'uuid';
    /**
     * @var string[]
     */
    protected $casts = [
        'deactivated_at' => 'datetime:d.m.Y H:i:s',
        'registered_at' => 'datetime:d.m.Y H:i:s',
        'lastactive_at' => 'datetime:d.m.Y H:i:s'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];
    /**
     * @var Collection|mixed
     */
    public Collection $forumPermissions;

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

    /**
     * @return BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class, 'users_has_groups', 'user_uuid', 'group_uuid');
    }

    /**
     * @return HasMany
     */
    public function invites(): HasMany
    {
        return $this->hasMany(Invite::class, 'created_uuid', 'uuid');
    }

    /**
     * @param array $groups
     * @return bool
     */
    public function isInGroup(array $groups): bool
    {
        $isInGroup = false;
        foreach($groups as $group) {
            $isInGroup = $this->groups->contains('group', $group);
            if($isInGroup) break;
        }

        return $isInGroup;
    }

    /**
     * @return Attribute
     */
    public function emailEncrypted(): Attribute
    {
        return Attribute::make(
            get: fn($value) => decrypt($value),
            set: fn($value) => encrypt($value)
        );
    }

    /**
     * @return Attribute
     */
    public function emailHashed(): Attribute
    {
        return Attribute::make(
            set: fn($value) => hash('sha512', $value)
        );
    }
}
