<?php

namespace Modules\Service\Services;

use Modules\Feature\Enums\FeatureTypeEnum;
use Modules\Feature\Services\FeatureService;
use Modules\Service\Models\Service;
use Modules\Service\Interfaces\ServiceRepositoryInterface;

class ServiceService
{
    public function __construct(
        private ServiceRepositoryInterface $serviceRepository,
        private FeatureService $featureService
    )
    {}

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

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

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

    public function getItem($id)
    {
        $service = $this->checkItem($id);
        $serviceDescription = $this->serviceRepository->getItem($id);
        foreach ($serviceDescription as $row) {
            $service[$row->local]= $row;
        }
        return $service;
    }

    public function getItemTrashed($id)
    {
        $service = $this->serviceRepository->getItemTrashed($id);
        return $service;
    }

    public function create(array $request): service
    {
        $service = $this->serviceRepository->create($request);
        $this->serviceRepository->saveData($request,$service->id);
        if(isset($request['features']))
        {
            $features['features'] = $request['features'];
            $features['type'] = FeatureTypeEnum::SERVICE->value;
            $features['id'] = $service->id;

            $this->featureService->create($features);
        }
        return $service;
    }
    public function update(array $request,int $id): service
    {
        $service = $this->checkItem($id);


        $service = $this->serviceRepository->update($request,$service);
        $this->serviceRepository->saveData($request,$service->id);

        $this->featureService->deleteAll([
            'type'=>FeatureTypeEnum::SERVICE->value,
            'id'=>$id
        ]);
        if(isset($request['features']))
        {
            $features['features'] = $request['features'];
            $features['type'] = FeatureTypeEnum::SERVICE->value;
            $features['id'] = $service->id;

            $this->featureService->create($features);
        }
        return $service;
    }
    public function destroy(int $id): void
    {
        $service = $this->checkItem($id);
        if ($service)
        {
            $this->serviceRepository->destroy($service);
        }
    }

    public function restore(int $id): void
    {
        $service = $this->getItemTrashed($id);
        if ($service)
        {
            $this->serviceRepository->restore($service);
        }
    }

    public function forceDelete(int $id): void
    {
        $service = $this->getItemTrashed($id);
        if ($service)
        {
            $this->serviceRepository->forceDelete($service);
        }
    }

    public function forceDeleteAll(array $ids): void
    {
        $this->serviceRepository->forceDeleteAll($ids);
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->serviceRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->serviceRepository->restoreAll($request['ids']);
        }
    }
}
