<?php

namespace Modules\User\Repositories;

use App\Models\UserModule;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
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\UserDisableColumn;
use Modules\User\Models\UserSector;
use Modules\User\Models\UserVisit;
use Modules\User\Models\Visitor;

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

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

    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(int $id)
    {
        $user = User::onlyTrashed()->find($id);
        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 sectors(int $id)
    {
        return UserSector::where('user_id',$id)->cursor();
    }

    public function saveData(array $request,$user_id)
    {
        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 saveSectors(array $request,$user_id)
    {
        UserSector::where('user_id',$user_id)->delete();
        if (@$request['sector_id'])
        {
            foreach ($request['sector_id'] as $sector_id)
            {
                UserSector::create([
                    'sector_id'=>$sector_id,
                    'user_id'=>$user_id
                ]);
            }
        }
    }

    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 disableColumn(array $request)
    {
        return UserDisableColumn::query()
            ->where('user_id',$request['user_id'])
            ->where('column',$request['column'])
            ->where('module',$request['module'])
            ->first();
    }

    public function deleteColumn($item)
    {
        $item->delete();
    }

    public function addColumn(array $request)
    {
        UserDisableColumn::create([
            'user_id'=>$request['user_id'],
            'column'=>$request['column'],
            'module'=>$request['module'],
        ]);
    }

    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 $ids) :void
    {
         User::whereIn('id', $ids)->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 $ids) :void
    {
        User::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
