<?php

namespace Modules\Currency\Services;

use Illuminate\Http\Request;
use Modules\Currency\Interfaces\CurrencyRepositoryInterface;
use Modules\Currency\Models\Currency;

class CurrencyService
{
    private CurrencyRepositoryInterface $currencyRepository ;

    public function __construct(CurrencyRepositoryInterface $currencyRepository)
    {
        $this->currencyRepository = $currencyRepository;
    }

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

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

        if (@$request['date_to'])
        {
            $builder->whereDate('currencies.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']) {
            $currencies= $builder->paginate(50);
        }else{
            $currencies=$builder->cursor();
        }
        return $currencies;
    }

    public function checkItem($id)
    {
        $currency = $this->currencyRepository->checkItem($id);
        return $currency;
    }

    public function getItem($id)
    {
        $currency = $this->checkItem($id);
        $currencyDescription = $this->currencyRepository->getItem($id);
        foreach ($currencyDescription as $row) {
            $currency[$row->local]= $row;
        }
        return $currency;
    }

    public function getItemTrashed($id)
    {
        $currency = $this->currencyRepository->getItemTrashed($id);
        return $currency;
    }

    public function create(array $request): Currency
    {
        $currency = $this->currencyRepository->create($request);
        $this->currencyRepository->saveData($request,$currency->id);
        return $currency;
    }
    public function update(array $request,int $id): Currency
    {
        $currency = $this->checkItem($id);

        $currency = $this->currencyRepository->update($request,$currency);

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