<?php

namespace TPS\Shop\Repositories;
use Carbon\Carbon;
use Webkul\Core\Repositories\SliderRepository as SliderRepo;
class SliderRepository extends SliderRepo
{
    public function getActiveSliders()
    {
        $currentChannel = core()->getCurrentChannel();

        $currentLocale = core()->getCurrentLocale();

        return $this->where('channel_id', $currentChannel->id)
            ->whereRaw("find_in_set(?, locale)", [$currentLocale->code])
            ->where(function ($query) {
                $query->where('expired_at', '>=', Carbon::now()->format('Y-m-d'))
                    ->orWhereNull('expired_at');
            })
            ->orderBy('sort_order', 'ASC')
            ->get();
    }

    public function getWeeklyOffers()
    {
        $results = app(ProductFlatRepository::class)->scopeQuery(function ($query) {

        $locale = core()->getRequestedLocaleCode();

        $qb = $query->distinct()
            ->where('channel', core()->getCurrentChannelCode())
            ->where('product_flat.locale', $locale)
            ->whereNotNull('special_price')
            ->whereNotNull('special_price_to')
            ->whereNotNull('special_price_from')
            ->whereDate('special_price_from','<=',Carbon::today())
            ->whereDate('special_price_to','>=',Carbon::today())
            ->limit(8);

        return $qb->inRandomOrder();

        })->get();

        return $results;
    }
}