<?php

namespace Modules\User\Services;

use Illuminate\Http\Request;
use Modules\User\Interfaces\UserRepositoryInterface;
use Modules\User\Models\User;
use Modules\User\Enums\UserRoleEnum;

class UserService
{
    private UserRepositoryInterface $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function get(array $request = [])
    {
        $builder = $this->userRepository->get($request);

        if (@$request['name']) {
            $builder->where('name', 'LIKE', '%' . $request['name'] . '%');
        }
        if (@$request['fullname']) {
            $builder->where('name', 'LIKE', '%' . $request['fullname'] . '%')->orWhere('last_name', 'LIKE', '%' . $request['fullname'] . '%');
        }
        if (@$request['email']) {
            $builder->where('email', $request['email']);
        }
        if (@$request['phone']) {
            $builder->where('phone', $request['phone']);
        }
        if (@$request['parent_id']) {
            $builder->where('parent_id', $request['parent_id']);
        }
        if (@$request['canteen_id']) {
            $builder->where('canteen_id', $request['canteen_id']);
        }
        if (@$request['id_number']) {
            $builder->where('id_number', $request['id_number']);
        }
        if (isset($request['have_cash'])) {
            if ($request['have_cash'] == 0) {
                $builder->where('wallet', 0);
            } else {
                $builder->where('wallet', '>', 0);
            }
        }
        if (isset($request['role'])) {
            if (is_array($request['role'])) {
                $builder->whereIn('role', $request['role']);
            } else {
                $builder->where('role', $request['role']);
            }
        }
        if (isset($request['in_roles'])) {
            $builder->whereIn('role', $request['in_roles']);

        }

        if (@$request['date_from']) {
            $builder->whereDate('created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to']) {
            $builder->whereDate('created_at', '<=', $request['date_to']);
        }

        if (isset($request['status'])) {
            if (@$request['status'] == 0) {
                $builder->onlyTrashed();
            }
        }

        if (@$request['orderBy']) {
            $builder->orderBy($request['orderBy']['key'], $request['orderBy']['value']);
        } else {
            $builder->orderBy('id', 'DESC');
        }
        if (@$request['trashed']) {
            $users = $builder->withTrashed()->first();
        } elseif (@$request['paginate']) {
            $users = $builder->paginate(50);
        } elseif (@$request['api']) {
            $users = $builder;
        } else {
            $users = $builder->get();
        }
        // dd($users->toArray());
        return $users;
    }
    public function checkItem($id)
    {
        $user = $this->userRepository->checkItem($id);
        return $user;
    }

    public function getItem($id)
    {
        return $this->userRepository->getItem($id);
    }

    public function getItemTrashed($id)
    {
        $user = $this->userRepository->getItemTrashed($id);
        return $user;
    }

    public function create(array $request): User
    {
        $user = $this->userRepository->create($request);
        $this->userRepository->saveData($request, $user->id);
        if(!isset($request['photo']))
        {
            if($user->role == UserRoleEnum::PARENT->value)
            {
               $photo = 'users/Parent.png';
            }
            elseif($user->role == UserRoleEnum::CHILD->value)
            {
                $photo = 'users/Student.png';
            }
            elseif($user->role == UserRoleEnum::CANTEEN->value)
            {
                $photo = 'users/Market.png';
            }
            else
            {
                $photo =  'logo.png';
            }
            $this->update(['photo'=>$photo],$user->id);
        }
        return $user;
    }
    public function update(array $request, int $id): User
    {
        $user = $this->checkItem($id);
        $user = $this->userRepository->update($request, $user);
        $this->userRepository->saveData($request, $user->id);
        $this->userRepository->saveDataCanteen($request, $user->id);
        return $user;
    }
    public function destroy(int $id): void
    {
        $user = $this->checkItem($id);
        if ($user) {
            $this->userRepository->destroy($user);
        } else {
            $user = $this->getItemTrashed($id);
            $this->userRepository->restore($user);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->userRepository->destroyAll($request['ids']);
        } else {
            $this->userRepository->restoreAll($request['ids']);
        }
    }

    public function canteenUser($canteen_id)
    {
        return $this->userRepository->canteenUser($canteen_id);
    }
}
