<?php

namespace app\event;


use app\service\GoogleAdsGroupService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;

use support\Response;
use DI\Annotation\Inject;
use Webman\RedisQueue\Redis;

//use QL\QueryList;


class GoogleAdsGroups
{
    /**
     * @Inject
     * @var GoogleOAuthService
     */

    private $googleOAuthService;


    const event = 'googleads:groups:event';
    const queue = 'googleads:groups:queue';


    public function syncGroups($options = [])
    {
        $queue     = self::queue;
        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
        foreach ($customers as $customer) {
            if ($customer['login_customer_id'] > 0) {
                Redis::send($queue,$customer,rand(1,10));
            }
        }
        return self::event.' send queue ok';
    }

    /**
     * get groups
     * @throws ApiException
     */
    public function getGroups($customer)
    {

        $googleAdsGroupService = new GoogleAdsGroupService($customer['customer_id']);
        if ($customer['login_customer_id'] > 0) {
            $resourceName = $googleAdsGroupService->runListGroups($customer['customer_id'], $customer);
        }
//        return $this->successResponse(['groups_list' => $resourceName]);
    }

    // 可以加入一些公共方法
    protected
    function successResponse($data): Response
    {
        return Json([
            'code' => 0,
            'msg' => 'ok',
            'data' => $data,
        ]);
    }

    protected
    function errorResponse($code, $message, $data = []): Response
    {
        return Json([
            'code' => $code,
            'msg' => $message ?: 'error',
            'data' => $data
        ]);
    }
}