<?php

namespace Modules\Warehouse\Services;

use Illuminate\Http\Request;
use Modules\Warehouse\Interfaces\WarehouseRepositoryInterface;
use Modules\Warehouse\Models\Warehouse;

class WarehouseService
{
    private WarehouseRepositoryInterface $warehouseRepository ;

    public function __construct(WarehouseRepositoryInterface $warehouseRepository)
    {
        $this->warehouseRepository = $warehouseRepository;
    }

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

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

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

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

        if (@$request['warehouse_id'])
        {
            if ($request['warehouse_id'] == 'main')
            {
                $builder->whereNull('warehouses.warehouse_id');
            }
            else
            {
                $builder->where('warehouses.warehouse_id', $request['warehouse_id']);
            }
        }

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

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

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['paginate']) {
            $warehouses= $builder->paginate(50);
        }else{
            $warehouses=$builder->cursor();
        }
        return $warehouses;
    }
    public function checkItem($id)
    {
        $warehouse = $this->warehouseRepository->checkItem($id);
        return $warehouse;
    }

    public function getItem($id)
    {
        $warehouse = $this->checkItem($id);
        $warehouseDescription = $this->warehouseRepository->getItem($id);
        foreach ($warehouseDescription as $row) {
            $warehouse[$row->local]= $row;
        }
        return $warehouse;
    }

    public function getItemTrashed($id)
    {
        $warehouse = $this->warehouseRepository->getItemTrashed($id);
        return $warehouse;
    }

    public function create(array $request): Warehouse
    {
        $warehouse = $this->warehouseRepository->create($request);
        $this->warehouseRepository->saveData($request,$warehouse->id);
        return $warehouse;
    }
    public function update(array $request,int $id): Warehouse
    {
        $warehouse = $this->checkItem($id);


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