<?php

namespace Modules\Subscription\Services;

use Illuminate\Http\Request;
use Modules\Subscription\Interfaces\SubscriptionRepositoryInterface;
use Modules\Subscription\Models\Subscription;

class SubscriptionService
{
    private SubscriptionRepositoryInterface $subscriptionRepository ;

    public function __construct(SubscriptionRepositoryInterface $subscriptionRepository)
    {
        $this->subscriptionRepository = $subscriptionRepository;
    }

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

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

        if (@$request['store'])
        {
            $builder->whereHas('store.currentDescription', function ($query) use($request){
                $query->where('title','LIKE', '%'.$request['store'].'%');
            });
        }

        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['paginate']) {
            $subscriptions= $builder->paginate(50);
        }else{
            $subscriptions=$builder->cursor();
        }
        return $subscriptions;
    }

    public function subscriptionsNumber()
    {
        return [1,2,3,4,5];
    }

    public function checkItem($id)
    {
        $subscription = $this->subscriptionRepository->checkItem($id);
        return $subscription;
    }

    public function getItem($id)
    {
        $subscription = $this->checkItem($id);

        return $subscription;
    }

    public function getItemTrashed($id)
    {
        $subscription = $this->subscriptionRepository->getItemTrashed($id);
        return $subscription;
    }

    public function create(array $request): Subscription
    {
        $subscription = $this->subscriptionRepository->create($request);
        $this->subscriptionRepository->saveData($request,$subscription->id);
        return $subscription;
    }
    public function update(array $request,int $id): Subscription
    {
        $subscription = $this->checkItem($id);


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