<?php

namespace app\event;

use app\service\BpsAdAccountService;
use app\service\GoogleAdsAssetService;
use app\service\GoogleOAuthService;
use Google\ApiCore\ApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use support\Db;
use Webman\RedisQueue\Redis;
use support\Response;
use DI\Annotation\Inject;

//use QL\QueryList;


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

    private $googleOAuthService;


    /**
     * @Inject
     * @var BpsAdAccountService
     */

    private $bpsAdAccountService;


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

    public function syncCreatives($options = [])
    {
        $queue     = self::queue;
//        $customers = $this->googleOAuthService->getGoogleAdCustomers($options);
        $customers = $this->bpsAdAccountService->getGoogleAdAccounts($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 assets
     * @throws ApiException
     */
    public function getCreatives($customer)
    {
            $googleAdsAssetService = new GoogleAdsAssetService($customer['account_id']);
            if ($customer['login_customer_id'] >0){
                $resourceName = $googleAdsAssetService->runListCreatives($customer['account_id'],$customer);
            }
//        return $this->successResponse(['ads_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
        ]);
    }
}