<?php

namespace Modules\Warehouse\Services;

use Modules\City\Services\CityService;
use Modules\Warehouse\Interfaces\WarehouseRepositoryInterface;
use Modules\Warehouse\Models\Warehouse;

class WarehouseService
{

    public function __construct(
        private WarehouseRepositoryInterface $warehouseRepository ,
        private CityService $cityService
    )
    {}

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

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

        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(int $id)
    {
        $warehouse = $this->checkItem($id);
        $warehouseDescription = $this->warehouseRepository->getItem($id);
        foreach ($warehouseDescription as $row) {
            $warehouse[$row->local]= $row;
        }
        return $warehouse;
    }

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

    public function regionsId(int $id)
    {
        $warehouse = $this->getItem($id);
        $citiesId = $this->warehouseRepository->citiesId($warehouse);
        $region_ids = $this->cityService->get(['ids'=>$citiesId])->pluck('region_id')->unique()->toArray();
        return $region_ids;
    }

    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);
        if (isset($request['city_id']))
        {
            $this->warehouseRepository->updateCities($request,$warehouse);
        }
        return $warehouse;
    }
    public function update(array $request,int $id): Warehouse
    {
        $warehouse = $this->checkItem($id);

        $warehouse = $this->warehouseRepository->update($request, $warehouse);
        if (isset($request['title_ar']))
        {
            $this->warehouseRepository->saveData($request,$warehouse->id);
        }
        if (isset($request['city_id']))
        {
            $this->warehouseRepository->updateCities($request,$warehouse);
        }
        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']);
        }
    }
}
