<?php

namespace Modules\User\Repositories;

use App\Models\UserModule;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Interfaces\UserRepositoryInterface;
use Modules\User\Models\User;
use Modules\User\Models\UserCard;
use Modules\User\Models\UserCode;
use Modules\User\Models\UserDescription;
use Modules\User\Models\UserProviderProfile;
use Modules\User\Models\UserVisit;
use Modules\User\Models\Visitor;

class UserRepository implements UserRepositoryInterface
{
    public function get(array $request)
    {
        $builder = User::select('*');
        return $builder;
    }

    public function getItem(int $id)
    {
        return User::find($id);
    }

    public function getItemByCode(string $code)
    {
        return User::where('code', $code)->first();
    }

    public function checkItem(int $id)
    {
        return User::find($id);
    }

    public function getCard(int $id)
    {
        return UserCard::find($id);
    }
    public function deleteCard(UserCard $card)
    {
        return $card->delete();
    }

    public function cards(int $id)
    {
        return UserCard::where('user_id',$id)->cursor();
    }
    public function createCard(array $request)
    {
        return UserCard::create($request);
    }

    public function getItemTrashed(string $code)
    {
        $user = User::onlyTrashed()->where('code', $code)->first();
        return $user ;
    }

    public function createVisit($request)
    {
        UserVisit::create($request);
    }

    public function create(array $request): User
    {
        $user = User::create($request);
        return $user;
    }

    public function update(array $request, User $user): User
    {
        $user->update($request);

        return $user;
    }

    public function updateCard(array $request, UserCard $card): UserCard
    {
        $card->update($request);

        return $card;
    }

    public function saveModules(array $request, User $user)
    {
        UserModule::where('user_id',$user->id)->delete();
        if (@$request['modules'])
        {
            foreach ($request['modules'] as $module_id)
            {
                UserModule::create([
                    'module_id'=>$module_id,
                    'user_id'=>$user->id
                ]);
            }
        }
    }

    public function saveProfile(array $request, User $user)
    {
        UserProviderProfile::updateOrCreate([
            'user_id'=>$user->id
        ],[
            'status'=> $request['status'],
            'is_top'=> $request['is_top'] ?? 0,
            'is_reliable'=> $request['is_reliable'] ?? 0,
        ]);
        if ($request['is_top'] ?? 0) {
            UserProviderProfile::where('user_id', '!=', $user->id)->update(['is_top' => 0]);
        }
    }

    public function getItemByEmail(string $email)
    {
        return User::where('email',$email)->first();
    }

    public function getItemByToken(string $token)
    {
        return User::where('api_token',$token)->first();
    }

    public function createToken(User $user)
    {
        $token = md5(time().rand(1000,9999));
        $user->update(['api_token'=>$token]);
        return $user;
    }

    public function getVisitorByDeviceToken(string $token)
    {
        return Visitor::where('device_token',$token)->first();
    }

    public function sendCode(array $request)
    {
        UserCode::create($request);
    }

    public function checkCode(array $request)
    {
        $code = UserCode::where('user',$request['email'])->orderBy('id','DESC')->first();
        if (($code->code ?? null) != $request['code'])
        {
            $code = null;
        }
        return $code;
    }

    /**
     * Remove the specified user from storage.
     * @param User $user
     * @return void
     */
    public function destroy(User $user) :void
    {
        $user->delete();
    }

    /**
     * Remove several users by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $codes) :void
    {
        $notRoles= [UserRoleEnum::SUPER_ADMIN->value, UserRoleEnum::ADMIN->value];
        User::whereIn('code', $codes)
        ->whereNotIn('role', $notRoles)
        ->where('id', '!=', auth()->Id())
        ->delete();
    }

    /**
     * Restore the specified user from storage.
     *
     * @param User $user
     * @return void
     */
    public function restore(User $user) :void
    {
        $user->restore();
    }

    /**
     * Restore several users by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $codes) :void
    {
        User::whereIn('code', $codes)->restore();
    }
}
