<?php

namespace Modules\Supplier\Services;

use Illuminate\Http\Request;
use Modules\Supplier\Interfaces\SupplierRepositoryInterface;
use Modules\Supplier\Models\Supplier;

class SupplierService
{
    private SupplierRepositoryInterface $supplierRepository ;

    public function __construct(SupplierRepositoryInterface $supplierRepository)
    {
        $this->supplierRepository = $supplierRepository;
    }

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

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

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

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

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

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

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

    public function getItem($id)
    {
        $supplier = $this->checkItem($id);
        $supplierDescription = $this->supplierRepository->getItem($id);
        foreach ($supplierDescription as $row) {
            $supplier[$row->local]= $row;
        }
        return $supplier;
    }

    public function getItemTrashed($id)
    {
        $supplier = $this->supplierRepository->getItemTrashed($id);
        return $supplier;
    }

    public function create(array $request): Supplier
    {
        $supplier = $this->supplierRepository->create($request);
        $this->supplierRepository->saveData($request,$supplier->id);
        return $supplier;
    }
    public function update(array $request,int $id): Supplier
    {
        $supplier = $this->checkItem($id);


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