<?php

/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Examples\Reports;

use Examples\Utils\DateUtils;

/**
 * This example illustrates how to generate a mediation report.
 */
class GenerateMediationReportToDay
{
    /**
     * Generates a mediation report.
     *
     * @param $service Google_Service_AdMob AdMob service object on which to
     *     run the requests.
     * @param account_name which follows the format "accounts/pub-XXXXXXXXXXXXXXXX".
     */
	public static function run($service, $accountName)
    {
  
        // Generate mediation report.
        $mediationReportRequest = self::createMediationReportRequest();
        $mediationReportResponse = $service->accounts_mediationReport->generate(
            $accountName,
            $mediationReportRequest
        );

        // Convert mediation report response to a simple object.
        $mediationReportResponse = $mediationReportResponse->tosimpleObject();
        return json_encode($mediationReportResponse);
    }

    /**
     * Generates a mediation report request.
     */
    public static function createMediationReportRequest()
    {
        /*
         * AdMob API only supports the account default timezone and
         * "America/Los_Angeles", see
         * https://developers.google.com/admob/api/v1/reference/rest/v1/accounts.mediationReport/generate
         * for more information.
         */
        $startDate = DateUtils::today();
        $endDate = DateUtils::today();

        // Specify date range.
        $dateRange = new \Google_Service_AdMob_DateRange();
        $dateRange->setStartDate($startDate);
        $dateRange->setEndDate($endDate);

$localizationSettings = new \Google_Service_AdMob_LocalizationSettings();
$localizationSettings->setcurrencyCode("EUR");
$localizationSettings->setlanguageCode("en-US");

        // Create mediation report specification.
        $reportSpec = new \Google_Service_AdMob_MediationReportSpec();
        $reportSpec->setMetrics(['ESTIMATED_EARNINGS','OBSERVED_ECPM','AD_REQUESTS','CLICKS','IMPRESSIONS','IMPRESSION_CTR','MATCHED_REQUESTS','MATCH_RATE']);
        $reportSpec->setDimensions(['APP']);
        $reportSpec->setDateRange($dateRange);
$reportSpec->setlocalizationSettings($localizationSettings);
        // Create mediation report request.
        $mediationReportRequest = new \Google_Service_AdMob_GenerateMediationReportRequest();
        $mediationReportRequest->setReportSpec($reportSpec);

        return $mediationReportRequest;
    }
}
