<?php

namespace Modules\Product\Services;

use Modules\MainService\Views\ViewFactory;
use Modules\Product\Interfaces\ProductRepositoryInterface;
use Modules\Product\Models\Product;

class ProductService
{
    public function __construct(
        private ProductRepositoryInterface $productRepository,
        private ViewFactory $viewFactory
    ){}

    public function renderView($request = [])
    {
        $request['products'] = $this->get($request);
        return $this->render($request);
    }

    public function render($request = [])
    {
        return $this->viewFactory
            ->render($request['view'])
            ->renderView($request);
    }

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

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

        if (@$request['ids'])
        {
            $builder->whereIn('products.id',$request['ids']);
        }

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

        if (@$request['date_to'])
        {
            $builder->whereDate('products.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']) {
            $products= $builder->paginate(is_numeric($request['paginate'])? $request['paginate'] : 50);

        }else{
            $products=$builder->get();
        }

        return $products;
    }


    public function checkItem($id)
    {
        $product = $this->productRepository->checkItem($id);
        return $product;
    }

    public function getItem($id)
    {
        $product = $this->checkItem($id);
        $productDescription = $this->productRepository->getItem($id);
        foreach ($productDescription as $key=>$row)
        {
            $product[$row->local]= $row;
        }
        $product['title'] = $product[getCurrentLocale()]->title;
        $product['description'] = $product[getCurrentLocale()]->description;
        return $product;
    }


    public function getItemTrashed($id)
    {
        $product = $this->productRepository->getItemTrashed($id);
        return $product;
    }



    public function create(array $request): Product
    {
        $product = $this->productRepository->create($request);
        $this->productRepository->saveData($request,$product->id);
        return $product;
    }
    public function update(array $request,int $id): Product
    {
        $product = $this->checkItem($id);
        $product = $this->productRepository->update($request,$product);
        if (isset($request['title_en']))
        {
            $this->productRepository->saveData($request,$product->id);
        }
        return $product;
    }
    public function updateLock($id)
    {
        $product = $this->checkItem($id);
        $this->update(['lock'=>($product->lock?0:1)],$id);
    }

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

}
