<?php

namespace App\Repositories\Core\Auth;

use App\Models\Core\Auth\RoleUser;
use App\Models\Core\Auth\User;
use App\Repositories\Core\Upload;
use App\Traits\Model\Basic;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laratrust\Traits\LaratrustUserTrait;

class Users
{
    use Basic, LaratrustUserTrait;

    public static function isAdmin()
    {
        return self::hasRole('admin');
    }

    public static function getInfo($id = false)
    {
        $id = $id ? $id : self::getId();
        if (! $id) {
            return false;
        }
        $user = self::get($id);
        $data = $user->toArray();
        $data['name'] = $user->name;
        $data['avatar'] = self::getAvatar($id);
        $data['roles'] = $user->roles->pluck('id')->toArray();
        $data['permissions'] = $user->allPermissions()->pluck('id')->toArray();

        return $data;
    }

    public static function store($data)
    {
        if ($data['id'] ?? false) {
            unset($data['password']);
        }
        $user = $data['id'] ?? false ? self::update($data) : self::create($data);
        $user->roles()->sync(array_keys($data['roles'] ?? []));

        return $user;
    }

    public static function create($data)
    {
        $data['password'] = $data['password'] ?? Hash::make($data['password']);
        $data['remember_token'] = Str::random(32);
        $data['active'] = true;
        $user = User::create($data);
        PasswordSecurities::create($user->id);

        return $user;
    }

    public static function getId()
    {
        $user = self::getUser();

        return $user ? $user->id : false;
    }

    public static function getName($id = false)
    {
        $user = $id ? self::get($id) : self::getUser();

        return $user->first_name.' '.$user->last_name;
    }

    public static function getUsername($id = false)
    {
        return $id ? self::get($id)->username : self::getUser()->username;
    }

    public static function getUser()
    {
        return Auth::user();
    }

    public static function isNotConnected()
    {
        return ! self::isConnected();
    }

    public static function isConnected()
    {
        return Auth::check();
    }

    public static function getOptions()
    {
        return User::orderBy('name')->pluck('name', 'id')->toArray();
    }

    public static function delete($id)
    {
        $ret = RoleUser::byUser($id)->delete();

        return User::destroy($id);
    }

    public static function getListByRole($role)
    {
        return self::selectOptions()->orderBy('name')->whereRoleIs($role)->get();
    }

    public static function hasRole($role, $user = false)
    {
        $user = $user ? $user : self::getUser();

        return $user ? $user->hasRole($role) : false;
    }

    public static function getRoles($user = false)
    {
        $user = $user ? $user : self::getUser();

        return $user ? $user->roles->pluck('name')->toArray() : false;
    }

    public static function getAvatar($user_id)
    {
        $avatar = self::get($user_id)->avatar;
        if (! $avatar) {
            return '/assets/img/no-avatar.png';
        }
        $path = '/images/avatars/';

        return $path.$avatar;
    }

    public static function selectOptions()
    {
        return User::select('id', DB::raw("concat(last_name,' ',first_name) as name"));
    }

    public static function getByUsername($username)
    {
        return User::byUsername($username)->withTrashed()->first();
    }

    public static function toggleActive($id, $active)
    {
        return self::get($id)->update(['active' => $active]);
    }

    public static function uploadAvatar($request)
    {
        $targetDir = 'uploads';
        $file = $request->file('avatar_file');
        $data = Upload::getData($file);
        $file_uploaded = Upload::store($file, $targetDir);
        $tab = pathinfo($file_uploaded);

        return [
            'name' => $tab['basename'],
        ];
    }

    public static function updateAvatar($id, $avatar)
    {
        return User::find($id)->update(['avatar' => $avatar]);
    }

    public static function updatePassword($id, $password)
    {
        $password = Hash::make($password);

        return User::find($id)->update(['password' => $password]);
    }

    public static function getModel()
    {
        return User::query();
    }
}
