<?php

namespace Modules\Callback\Services;

use Exception;
use GuzzleHttp\Client;

/**
 * 抖音数据回调---新版---使用事件管理模式
 * ios加桌和普通抖音事件管理是一个回传逻辑
 * 开发文档：https://event-manager.oceanengine.com/docs/8650/h5_api_docs/
 */
class TiktokEventReportService
{
    /**
     * 充值用户事件类型
     */
    protected $charge_event_type = 'active_pay';
    /**
     * 注册用户事件类型
     */
    protected $register_event_type = 'active_register';
    /**
     * 激活事件类型
     */
    protected $active_event_type = 'active';
    /**
     * 次留事件类型
     */
    protected $rentention_event_type = 'next_day_open';
    /**
     * 付费ROI事件类型
     */
    protected $purchase_roi_event_type = 'purchase_roi';


    /**
     * 充值用户数据上报地址
     */
    protected $report_url = 'https://analytics.oceanengine.com/api/v2/conversion';

    private $user;

    public function __construct()
    {

    }

    /**
     * 付费上报
     */
    public function reportCharge($user, $amount=0)
    {
        return $this->report($this->report_url, [
            'event_type' => $this->charge_event_type,
            'context' => [
                'ad' => [
                    'callback' => $user->callback
                ]
            ],
            'timestamp' => time()
        ]);
    }

    /**
     * 注册上报
     */
    public function reportRegister(DouyinUser $user)
    {
        return $this->report($this->report_url, [
            'event_type' => $this->register_event_type,
            'context' => [
                'ad' => [
                    'callback' => $user->callback
                ]
            ],
            'timestamp' => time()
        ]);
    }

    /**
     * 次日留存上报
     */
    public function reportRentention()
    {
        return $this->report($this->report_url, [
            'event_type' => $this->rentention_event_type,
            'context' => [
                'ad' => [
                    'callback' => $this->user->callback
                ]
            ],
            'timestamp' => time()
        ]);
    }

    public function reportAddDesktop()
    {
        return [];
    }

    /**
     * 激活上报
     */
    public function reportActive($user)
    {
        return $this->report($this->report_url, [
            'event_type' => $this->active_event_type,
            'context' => [
                'ad' => [
                    'callback' => $user->callback
                ]
            ],
            'timestamp' => time()
        ]);
    }

    /**
     * 数据上报
     */
    public function report(string $url, array $query_params)
    {
        $result = false;
        $content = '';
        try {
            myLog('tiktok_event_report')->info('report_param_event_tiktok:' . json_encode($query_params));

            $client = new Client(['timeout' => 4]);
            $response = $client->request('post', $url, ['json' => $query_params]);

            $content = $response->getBody()->getContents();
            myLog('tiktok_event_report')->info('report_result_event_tiktok:' . json_encode($content, true));

            $status_code = $response->getStatusCode();
            if ($status_code == 200) {
                $result = true;
            } else {
                $result = false;
            }
        } catch (Exception $e) {
            myLog('tiktok_event_report')->error('event_tiktok_ept:' . json_encode($query_params) . ' ept:' . $e->getMessage());
        } finally {
            $query_params['report_url'] = $url;
            return [
                'result' => $result,
                'content' => $content,
                'query_params' => $query_params,
            ];
        }
    }

}
