<?php

namespace Modules\PaymentWay\Services;

use Illuminate\Http\Request;
use Modules\PaymentWay\Interfaces\PaymentWayRepositoryInterface;
use Modules\PaymentWay\Models\PaymentWay;

class PaymentWayService
{
    private PaymentWayRepositoryInterface $paymentWayRepository ;

    public function __construct(PaymentWayRepositoryInterface $paymentWayRepository)
    {
        $this->paymentWayRepository = $paymentWayRepository;
    }

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

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

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

    public function getItem($id)
    {
        $payment_way = $this->paymentWayRepository->getItem($id);

        return $payment_way;
    }

    public function getItemTrashed($id)
    {
        $payment_way = $this->paymentWayRepository->getItemTrashed($id);
        return $payment_way;
    }

    public function create(array $request): PaymentWay
    {
        $payment_way = $this->paymentWayRepository->create($request);
        $this->paymentWayRepository->saveData($request,$payment_way->id);
        return $payment_way;
    }
    public function update(array $request,int $id): PaymentWay
    {
        $payment_way = $this->checkItem($id);


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