<?php

namespace App\Jobs\GrowingIO;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Sentry\SentryLaravel\SentryFacade;

abstract class BaseReportJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public const FEED_MOCK = "feed_mock";

    public const SIGN = 'sign';

    public const SIGN_FLOWER = 'sign_flower';

    public const FRIEND_INVITATION = 'friend_invitation';

    public const ACTIVATE_SVIP = 'activate_svip';

    public const VIP_EVENT = 'vip_event';

    public const TESTING = 'testing';

    public const EACH_LIKE = 'each_like_event';

    public const FEED_EACH_LIKE = 'feed_each_like_event';

    public const PAIR_SUCCESS = 'pair_success_event';

    public const CLICK_WEIXIN = 'click_weixin_event';

    public function handle()
    {
        try {
            $this->doHandle();
        } catch (\Throwable $exception) {
            SentryFacade::captureException($exception);
            Log::error($exception);
        }
    }

    abstract public function doHandle();

    protected function getCurrentTime()
    {
        return round(microtime(true) * 1000);
    }

    protected function doReport($event, $cs1, $var, $tm = null)
    {
        if (config('app.env') !== 'production') {
            return;
        }
        try {
            $client = new Client();
            $url = 'https://api.growingio.com/v3/826fb1882511d92d/s2s/cstm';

            $tm = $tm ?: $this->getCurrentTime();

            // 用户划卡片
            $response = $client->post($url, [
                'query' => [
                    'stm' => $tm
                ],
                'json' => [
                    [
                        'cs1' => (string)$cs1,
                        'tm' => $tm,
                        'n' => $event,
                        't' => 'cstm',
                        'var' => $var,
                    ]
                ]
            ]);
        } catch (\Throwable $exception) {
            SentryFacade::captureException($exception);
            \Log::error($exception);
        }
    }

    protected function transformGender($gender)
    {
        switch ($gender) {
            case 1:
                return '男性';
            case 2:
                return '女性';
            default:
                return '未知';
        }
    }

    protected function transformOrderType($type)
    {
        switch ($type) {
            case 1:
                return '小fa充值';
            case 2:
                return '服务费';
            case 3:
                return '72小时';
            case 9:
                return '小花充值10朵';
            case 10:
                return '小花充值100朵';
            case 12:
                return '超级会员1个月';
            case 13:
                return '超级会员3个月';
            default:
                return '其他';
        }
    }
}
