<?php

namespace app\service;

use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\V18\Services\AdGroupOperation;
use Google\Ads\GoogleAds\V18\Services\AdOperation;
use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation;
use Google\Ads\GoogleAds\V18\Services\CampaignOperation;

class GoogleAdsSdkService
{
    protected $googleAdsClient;

    public function __construct()
    {
        // 使用 GoogleAdsClientBuilder 构建 GoogleAdsClient 实例
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
        $this->googleAdsClient = (new GoogleAdsClientBuilder())->fromFile()->build();
//        return $config;
//        $this->googleAdsClient = new GoogleAdsClient([
//            'developerToken' => $config['developerToken'],
//            'clientId' => $config['clientId'],
//            'clientSecret' => $config['clientSecret'],
//            'refreshToken' => $config['refreshToken'],
//        ]);
    }

    // 创建广告预算
    public function createCampaignBudget($customerId, $budgetName, $amount): ?string
    {
        $campaignBudgetService = $this->googleAdsClient->getCampaignBudgetServiceClient();

        $campaignBudget = new \Google\Ads\GoogleAds\V18\Resources\CampaignBudget([
            'name' => $budgetName,
            'amount_micros' => $amount * 1000000,
//            'delivery_method' => 'STANDARD',
//            'status' => 'ENABLED',
        ]);

        $operation = new CampaignBudgetOperation();
        $operation->setCreate($campaignBudget);

        try {
            $response = $campaignBudgetService->mutateCampaignBudgets($customerId, [$operation]);
            return $response->getResults()[0]->getResourceName();
        } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
            return $e->getMessage();
        }
    }

    // 创建广告系列
    public function createCampaign($customerId, $budgetId, $campaignName)
    {
        $campaignService = $this->googleAdsClient->getCampaignServiceClient();

        $campaign = new \Google\Ads\GoogleAds\V18\Resources\Campaign([
            'name' => $campaignName,
            'advertisingChannelType' => 'SEARCH',
            'status' => 'PAUSED',
            'startDate' => '2024-12-01',
            'endDate' => '2024-12-31',
            'manualCpc' => new \Google\Ads\GoogleAds\V18\Resources\ManualCpc([
                'enhancedCpcEnabled' => true
            ]),
            'campaignBudget' => "customers/{$customerId}/campaignBudgets/{$budgetId}"
        ]);

        $operation = new CampaignOperation();
        $operation->setCreate($campaign);

        try {
            $response = $campaignService->mutateCampaigns($customerId, [$operation]);
            return $response->getResults()[0]->getResourceName();
        } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
            return $e->getMessage();
        }
    }

    // 创建广告组
    public function createAdGroup($customerId, $campaignId, $adGroupName, $cpcBidMicros)
    {
        $adGroupService = $this->googleAdsClient->getAdGroupServiceClient();

        $adGroup = new \Google\Ads\GoogleAds\V18\Resources\AdGroup([
            'name' => $adGroupName,
            'campaign' => "customers/{$customerId}/campaigns/{$campaignId}",
            'status' => 'PAUSED',
            'cpcBidMicros' => $cpcBidMicros,
            'startDate' => '2024-12-01',
            'endDate' => '2024-12-31',
        ]);

        $operation = new AdGroupOperation();
        $operation->setCreate($adGroup);

        try {
            $response = $adGroupService->mutateAdGroups($customerId, [$operation]);
            return $response->getResults()[0]->getResourceName();
        } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
            return $e->getMessage();
        }
    }

    // 创建广告
    public function createAd($customerId, $adGroupId, $adName, $headline, $description, $finalUrls)
    {
        $adService = $this->googleAdsClient->getAdServiceClient();

        // 创建文本广告
        $textAd = new \Google\Ads\GoogleAds\V18\Resources\AdTextAdInfo([
            'headline' => $headline,
            'description' => $description,
            'finalUrls' => $finalUrls,
        ]);

        $ad = new \Google\Ads\GoogleAds\V18\Resources\Ad([
            'name' => $adName,
            'adGroup' => "customers/{$customerId}/adGroups/{$adGroupId}",
            'status' => 'PAUSED',
            'textAd' => $textAd,
        ]);

        $operation = new AdOperation();
        $operation->setCreate($ad);

        try {
            $response = $adService->mutateAds($customerId, [$operation]);
            return $response->getResults()[0]->getResourceName();
        } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
            return $e->getMessage();
        }
    }
}