<?php

namespace Modules\Canteen\Services;

use Modules\Canteen\Interfaces\CanteenRepositoryInterface;
use Modules\Canteen\Models\Canteen;

class CanteenService
{
    private CanteenRepositoryInterface $canteenRepository ;

    public function __construct(CanteenRepositoryInterface $canteenRepository)
    {
        $this->canteenRepository = $canteenRepository;
    }

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

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }

        if (@$request['code'])
        {
            $builder->where('canteens.code', $request['code']);
        }

        if (@$request['school_id'])
        {
            $builder->where('canteens.school_id', $request['school_id']);
        }

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

        if (@$request['orderBy'])
        {
            if ($request['orderBy'] == 'ordersCount')
            {
                $builder->withCount('orders')->orderByDesc('orders_count');
            }
        }
        else
        {
            $builder->orderBy('id','DESC');
        }

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

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

        if (@$request['paginate']) {
            $canteens= $builder->paginate(is_numeric($request['paginate']) ? $request['paginate'] : 50 );
        }else{
            $canteens=$builder->cursor();
        }
        return $canteens;
    }
    public function checkItem($id)
    {
        $canteen = $this->canteenRepository->checkItem($id);
        return $canteen;
    }

    public function getItem($id)
    {
        $canteen = $this->checkItem($id);
        $canteenDescription = $this->canteenRepository->getItem($id);
        foreach ($canteenDescription as $row) {
            $canteen[$row->local]= $row;
        }
        return $canteen;
    }

    public function getItemByCode($code)
    {
        return $this->canteenRepository->getItemByCode($code);
    }

    public function getItemTrashed($id)
    {
        $canteen = $this->canteenRepository->getItemTrashed($id);
        return $canteen;
    }

    public function generateCode()
    {
        do
        {
            $code = rand(100,99999999999);
        }while(!is_null($this->getItemByCode($code)));
        return $code;
    }

    public function create(array $request): Canteen
    {
        $request['code'] = $this->generateCode();
        $canteen = $this->canteenRepository->create($request);
        $this->canteenRepository->saveData($request,$canteen->id);
        return $canteen;
    }
    public function update(array $request,int $id): Canteen
    {
        $canteen = $this->checkItem($id);


        $canteen = $this->canteenRepository->update($request,$canteen);
        $this->canteenRepository->saveData($request,$canteen->id);
        return $canteen;
    }
    public function destroy(int $id): void
    {
        $canteen = $this->checkItem($id);
        if ($canteen)
        {
            $this->canteenRepository->destroy($canteen);
        }
        else
        {
            $canteen = $this->getItemTrashed($id);
            $this->canteenRepository->restore($canteen);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->canteenRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->canteenRepository->restoreAll($request['ids']);
        }
    }
}
