<?php

namespace Modules\Bank\Services;

use Illuminate\Http\Request;
use Modules\Bank\Interfaces\BankRepositoryInterface;
use Modules\Bank\Models\Bank;

class BankService
{
    private BankRepositoryInterface $bankRepository ;

    public function __construct(BankRepositoryInterface $bankRepository)
    {
        $this->bankRepository = $bankRepository;
    }

    public function get(array $request)
    {
        $builder = $this->bankRepository->get($request);

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

        if (@$request['date_to'])
        {
            $builder->whereDate('banks.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['paginate']) {
            $banks= $builder->paginate(50);
        }else{
            $banks=$builder->cursor();
        }
        return $banks;
    }
    public function checkItem($id)
    {
        $bank = $this->bankRepository->checkItem($id);
        return $bank;
    }

    public function getItem($id)
    {
        $bank = $this->checkItem($id);
        $bankDescription = $this->bankRepository->getItem($id);
        foreach ($bankDescription as $row) {
            $bank[$row->local]= $row;
        }
        return $bank;
    }

    public function getItemTrashed($id)
    {
        $bank = $this->bankRepository->getItemTrashed($id);
        return $bank;
    }

    public function create(array $request): Bank
    {
        $bank = $this->bankRepository->create($request);
        $this->bankRepository->saveData($request,$bank->id);
        return $bank;
    }
    public function update(array $request,int $id): Bank
    {
        $bank = $this->checkItem($id);


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