<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Yajny\Controller\CouponCtrl;
use Yajny\Controller\StoreCtrl;
use Yajny\Models\Category;
use Yajny\Models\Competition;
use Yajny\Models\Coupon;
use Yajny\Models\Event;
use Yajny\Models\Position;
use Yajny\Models\Store;
use Yajny\Models\StripBanner;
use Yajny\Models\Tag;

define("LUCKY_HOUR_START", "11:00am");
define("LUCKY_HOUR_END", "1:00pm");

class EventsController extends Controller
{

    public function getEventPage($slug)
    {
        $event = Event::getForApp($slug);
        if (!$event)
            abort(404);

        return $this->returnEventPageData($event);
    }

    private function returnEventPageData(Event $event)
    {
        $StoreCtrl = new StoreCtrl;
        $regionCode = request('region');
        $now = Carbon::now();

        $topStores = StoreCtrl::getTopStores(request('user_id'));
        $top_cashback_stores = $StoreCtrl->getStoresById(config('settingConfig.hp_new_top_stores_top_row')[$regionCode ?? 'global'] ?? [])->map(function ($item) {
            return Store::mapItems($item);
        });

        $hotCoupons = CouponCtrl::getUserRecommendedCouponsByType('coupon', request('region'), 5, $topStores->pluck('id'), null, true)->map(function ($item) {
            return Coupon::mapItems($item);
        });

        $positions = Position::getByLabel("event_{$event->slug}", $regionCode)->map(function ($position) {
            return Position::mapItemsForApp($position);
        });

        $showFlashSale = false;
        if ($event->flash_sale_days) {
            $flashSaleDays = json_decode($event->flash_sale_days);
            foreach ($flashSaleDays as $day) {
                if ($now->tz('Africa/Cairo')->toDateString() == $day)
                    $showFlashSale = true;
            }
        }

        $flashSaleOffers = [];
        if ($showFlashSale) {
            $flashSaleTag = Tag::where('slug', 'flash-sale')->first();
            $flashSaleOffers = CouponCtrl::getCouponsByTag('offer', $flashSaleTag->id)->map(function ($item) {
                return Coupon::mapItems($item);
            });
        }

        $storeCategories = Category::selectRaw(Category::colSelect())->orderBy('is_recommended')->orderBy('visits', 'DESC')->get()->map(function ($item) {
            return Category::mapItems($item);
        });

        //Lucky Hours from 1:00 PM to 3:00 PM Egyptian time (GMT +2)
        $luckyHourStart = Carbon::createFromTimeString(LUCKY_HOUR_START);
        $luckyHourEnd = Carbon::createFromTimeString(LUCKY_HOUR_END);

        $luckyHourStores = [];
        $luckyHourTimeLeft = null;
        if ($now->between($luckyHourStart, $luckyHourEnd)) {
            $luckyHourTag = Tag::where('slug', 'lucky-hour')->first();
            $luckyHourStores = StoreCtrl::getStoresById($luckyHourTag->recom_store ?? [])->map(function ($item) {
                return Store::mapItems($item);
            });

            $interval = $now->diffAsCarbonInterval($luckyHourEnd);
            $luckyHourTimeLeft = ['hours' => $interval->h, 'minutes' => $interval->i, 'seconds' => $interval->s];
        }
        $stripBanner = StripBanner::getForApp($regionCode ?? 'global');
        $event->increment('views_count');
        return [
            'event' => Event::getEventDataForApp($event),
            'category_hot_deals' => $storeCategories,
            'flash_sale_offers' => $flashSaleOffers,
            'lucky_hour_stores' => $luckyHourStores,
            'cashback_stores' => $top_cashback_stores->splice(0, 5),
            'hot_coupons' => $hotCoupons,
            'positions' => $positions,
            'lucky_hour_time_left' => $luckyHourTimeLeft,
            'competition' => Competition::getCompetitionDataForApp(),
            'strip' => $stripBanner ? StripBanner::mapItemsForApp($stripBanner) : null,
        ];
    }
}
