<?php

namespace App\Listeners;

use App\Events\GroupSetEvent;
use App\Models\Shop;
use App\Services\Business\BusinessFactory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Jobs\KttQueryGroupStatus;

class GroupQueryListener implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param GroupSetEvent $event
     * @return void
     */
    public function handle(GroupSetEvent $event)
    {
        $shops = Shop::query()
            ->where('plat_id', Shop::$PLAT_KTT)
            ->where('expires_at', '>', time())
            ->get(['id', 'plat_id'])
            ->toArray();
        foreach ($shops as $shop) {
            $client = BusinessFactory::init()->make($shop['plat_id'])->setShopWithId($shop['id']);
            $client->createGroup($event->groupId);
            KttQueryGroupStatus::dispatch($event->groupId, $shop['id'])->delay(5);
        }
    }
}
