<?php

namespace Modules\Package\Services;

use Illuminate\Http\Request;
use Modules\Feature\Enums\FeatureTypeEnum;
use Modules\Feature\Interfaces\FeatureRepositoryInterface;
use Modules\Feature\Services\FeatureService;
use Modules\Package\Interfaces\PackageRepositoryInterface;
use Modules\Package\Models\Package;

class PackageService
{
    public function __construct(
        private PackageRepositoryInterface $packageRepository,
        private FeatureService $featureService
    )
    {}

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

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

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

    public function getItem($id)
    {
        $package = $this->checkItem($id);
        $packageDescription = $this->packageRepository->getItem($id);
        foreach ($packageDescription as $row) {
            $package[$row->local]= $row;
        }
        return $package;
    }

    public function getItemTrashed($id)
    {
        $package = $this->packageRepository->getItemTrashed($id);
        return $package;
    }

    public function create(array $request): Package
    {
        $package = $this->packageRepository->create($request);
        $this->packageRepository->saveData($request,$package->id);
        if(isset($request['features']))
        {
            $features['features'] = $request['features'];
            $features['type'] = FeatureTypeEnum::PACKAGE->value;
            $features['id'] = $package->id;
            $this->featureService->create($features);
        }
        return $package;
    }
    public function update(array $request,int $id): Package
    {
        $package = $this->checkItem($id);


        $package = $this->packageRepository->update($request,$package);
        $this->packageRepository->saveData($request,$package->id);
        $this->featureService->deleteAll([
            'type'=>FeatureTypeEnum::SERVICE->value,
            'id'=>$id
        ]);
        if(isset($request['features']))
        {
            $features['features'] = $request['features'];
            $features['type'] = FeatureTypeEnum::PACKAGE->value;
            $features['id'] = $package->id;
            $this->featureService->create($features);
        }
        return $package;
    }
    public function destroy(int $id): void
    {
        $package = $this->checkItem($id);
        if ($package)
        {
            $this->packageRepository->destroy($package);
        }
        else
        {
            $package = $this->getItemTrashed($id);
            $this->packageRepository->restore($package);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->packageRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->packageRepository->restoreAll($request['ids']);
        }
    }
}
