<?php

namespace Modules\Offer\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Offer\Interfaces\OfferRepositoryInterface;
use Modules\Offer\Models\Offer;
use Modules\Offer\Models\OfferDescription;
use Modules\Offer\Models\OfferProduct;
use Modules\Product\Models\Product;

class OfferRepository implements OfferRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Offer::with(['store','category','subCategory'])
            ->join('offer_descriptions AS sd', 'offers.id', 'sd.offer_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'offers.*']);
        return $builder;
    }

    public function getItem(int $id)
    {

        $offer = Offer::with(['store','category','subCategory'])->find($id);

        $query = OfferDescription::where('offer_id', $offer->id)
            ->join('offers', 'offer_descriptions.city_id','offers.id')
            ->join('languages', 'languages.id', 'offer_descriptions.language_id')
            ->select(['offers.*','offer_descriptions.title', 'languages.local']);

        $offerDescription= $query->get();
        // dd($cityDescription->toArray());
        foreach ($offerDescription as $key=>$row) {

            $offer[$row->local] = $row;
        }
        return $offer;
    }

    public function checkItem(int $id)
    {
        return Offer::find($id);
    }

    public function getProductIds(Offer $offer)
    {
        return OfferProduct::where('offer_id',$offer->id)->pluck('product_id')->toArray();
    }

    public function productOffer(int $product_id)
    {
        $date = date('Y-m-d');
        $offer = Offer::query()
            ->join('offer_products', 'offers.id', 'offer_products.offer_id')
            ->where('offer_products.product_id',$product_id)
            ->whereDate('offers.start_date','<=',$date)
            ->whereDate('offers.end_date','>=',$date)
            ->first();
        return $offer;
    }

    public function getItemTrashed(int $id)
    {
        $offer = Offer::onlyTrashed()->find($id);
        return $offer ;
    }

    public function create(array $request): Offer
    {
        $offer = Offer::create($request);
        return $offer;
    }

    public function update(array $request, ?Offer $offer): Offer
    {
        $offer->update($request);

        return $offer;
    }

    public function saveProducts(array $request,$offer)
    {
        $offer->products()->sync($request['product_ids'] ?? []);
    }

    public function saveData(array $request, int $offer_id ) :void
    {
        OfferDescription::where('offer_id', $offer_id)->delete();
        foreach (languages() as $lang) {
            $data =
            [
                'offer_id'=> $offer_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
            ];
            OfferDescription::create($data);
        }
    }
    /**
     * Remove the specified offer from storage.
     * @param Offer $offer
     * @return void
     */
    public function destroy(Offer $offer) :void
    {
        $offer->delete();
    }

    /**
     * Remove several offers by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
         Offer::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified offer from storage.
     *
     * @param Offer $offer
     * @return void
     */
    public function restore(Offer $offer) :void
    {
        $offer->restore();
    }

    /**
     * Restore several offers by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Offer::whereIn('id', $ids)->onlyTrashed()->restore();
    }

    public function getOffersIdByProduct(array $request ,Product $product)
    {
        $offers = OfferProduct::join('offers', 'offers.id', 'offer_products.offer_id')
            ->where('offer_products.product_id',$product->id);
        if (isset($request['start_date']))
        {
            $offers->where('offers.start_date', '<=', $request['start_date']);
        }
        if (isset($request['start_date']))
        {
            $offers->where('offers.end_date', '>=', $request['end_date']);
        }

        return $offers->pluck('offers.id')->toArray();
    }
}
