<?php

namespace Modules\Offer\Services;

use Modules\Offer\Enums\OfferStatusEnum;
use Modules\Offer\Interfaces\OfferRepositoryInterface;
use Modules\Offer\Models\Offer;
use Modules\Product\Models\Product;
use Modules\Product\Services\ProductService;

class OfferService
{

    public function __construct(
        private OfferRepositoryInterface $offerRepository,
        private ProductService $productService,
    ){}

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

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

        if (@$request['type_id'])
        {
            $builder->whereHas('category', function ($query) use ($request) {
                $query->where('type_id', $request['type_id']);
            });

        }
        if (@$request['store_id'])
        {
            $builder->where('store_id', $request['store_id']);
        }
        if (@$request['dates'])
        {
            $builder->whereDate('offers.start_date', '<=',$request['dates']['start_date'])->whereDate('offers.end_date', '>=',$request['dates']['end_date']);

        }
        if (@$request['start_date'])
        {
            $builder->whereDate('offers.start_date',$request['start_date']);
        }
        if (@$request['category_id'])
        {
            if (is_array($request['category_id']))
            {
                $builder->whereIn('offers.category_id',$request['category_id']);
            }
            else
            {
                $builder->where('offers.category_id',$request['category_id']);
            }
        }

        if (@$request['sub_category_id'])
        {
            if (is_array($request['sub_category_id']))
            {
                $builder->whereIn('offers.sub_category_id',$request['sub_category_id']);
            }
            else
            {
                $builder->where('offers.sub_category_id',$request['sub_category_id']);
            }
        }
        if (@$request['warehouse_id'])
        {
            $builder->whereHas('products.warehouses',function ($query) use ($request){

                if (is_array($request['warehouse_id']))
                {
                    $query->whereIn('warehouse_id',$request['warehouse_id']);
                }
                else
                {
                    $query->where('warehouse_id',$request['warehouse_id']);
                }
            });

        }

        if (isset($request['typeStatus']))
        {
            $builder->where('offers.status',$request['typeStatus']);
        }

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

        if (@$request['date_to'])
        {
            $builder->whereDate('offers.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']) {
            $offers= $builder->paginate(50);
        }else{
            $offers=$builder->get();
        }
        return $offers;
    }
    public function checkItem($id)
    {
        $offer = $this->offerRepository->checkItem($id);
        return $offer;
    }

    public function getItem($id)
    {
        $offer = $this->offerRepository->getItem($id);

        return $offer;
    }

    public function getProduct($id)
    {
        $offer = $this->checkItem($id);
        $ids = $this->offerRepository->getProductIds($offer);
        $ids = count($ids) ? $ids : ['0'];
        return $this->productService->get(['ids'=>$ids]);
    }

    public function productOffer($product_id)
    {
        return $this->offerRepository->productOffer($product_id);
    }

    public function getItemTrashed($id)
    {
        $offer = $this->offerRepository->getItemTrashed($id);
        return $offer;
    }

    public function create(array $request): Offer
    {
        if($request['status'] == OfferStatusEnum::DAY->value)
        {
            $request['end_date'] = $request['start_date'];
        }
        $offer = $this->offerRepository->create($request);
        $this->offerRepository->saveData($request,$offer->id);
        $this->offerRepository->saveProducts($request,$offer);
        return $offer;
    }
    public function update(array $request,int $id): Offer
    {
        if($request['status'] == OfferStatusEnum::DAY->value)
        {
            $request['end_date'] = $request['start_date'];
        }
        $offer = $this->checkItem($id);


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

    public function checkOfferProduct(array $request,Product $product)
    {
        $sub_offer_ids = $this->get([
            'sub_category_id'=>$product->sub_category_id,
            'start_date'=>$request['start_date'] ?? null,
            'end_date'=>$request['end_date'] ?? null
        ])->pluck('id')->toArray();

        $offer_ids = $this->getOffersIdByProduct([
            'start_date'=>$request['start_date'] ?? null,
            'end_date'=>$request['end_date'] ?? null
        ],$product)->pluck('id')->toArray();
        $ids = array_unique( array_merge($sub_offer_ids,$offer_ids) );
        return $this->get(['ids'=>$ids]);
    }
}
