<?php

namespace Modules\Package\Services;

use Illuminate\Http\Request;
use Modules\Package\Interfaces\PackageRepositoryInterface;
use Modules\Package\Models\Package;

class PackageService
{
    private PackageRepositoryInterface $packageRepository;

    public function __construct(PackageRepositoryInterface $packageRepository)
    {
        $this->packageRepository = $packageRepository;
    }

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

        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);
        } elseif (@$request['api']) {
            $packages = $builder;
        } 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);
        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);
        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']);
        }
    }
}
