<?php

namespace Modules\Chip\Services;

use Modules\Chip\Interfaces\ChipRepositoryInterface;
use Modules\Chip\Models\Chip;

class ChipService
{
    private ChipRepositoryInterface $chipRepositoryInterface;

    public function __construct(ChipRepositoryInterface $chipRepositoryInterface)
    {
        $this->chipRepositoryInterface = $chipRepositoryInterface;
    }

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


        if (@$request['title']) {
            $builder->where('sd.title', 'LIKE', '%' . $request['title'] . '%');
        }
        if (@$request['cost']) {
            $builder->where('chips.cost', $request['cost']);
        }
        if (@$request['price']) {
            $builder->where('chips.cost', $request['price']);
        }

        if (@$request['date_from']) {
            $builder->whereDate('chips.created_at', '>=', $request['date_from']);
        }

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

    public function getItem($id)
    {
        $chip = $this->checkItem($id);
        $chipDescription = $this->chipRepositoryInterface->getItem($id);
        foreach ($chipDescription as $row) {
            $chip[$row->local] = $row;
        }
        return $chip;
    }

    public function getItemTrashed($id)
    {
        $chip = $this->chipRepositoryInterface->getItemTrashed($id);
        return $chip;
    }

    public function getItemByCode($code)
    {
        return $this->chipRepositoryInterface->getItemByCode($code);
    }



    public function create(array $request): Chip
    {
        $chip = $this->chipRepositoryInterface->create($request);
        $this->chipRepositoryInterface->saveData($request, $chip->id);
        return $chip;
    }
    public function update(array $request, int $id): Chip
    {

        $chip = $this->checkItem($id);
        $chip = $this->chipRepositoryInterface->update($request, $chip);
        $this->chipRepositoryInterface->saveData($request, $chip->id);
        return $chip;
    }
    public function updateMain(array $request, int $id): Chip
    {

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