<?php

namespace App\Services\Notice;

use App\Http\Controllers\Miniprogram\Core;
use App\Http\Controllers\Wechat\Kfaccount;
use App\Models\NoticeModel;
use App\Models\User\Openid;
use App\Models\User\UserModel;
use App\Services\Log\NoticeLogService;
use Illuminate\Support\Facades\Redis;

class UserNoticeService
{


    public function test(int $uid)
    {
        Redis::sadd("fpdx_supervip_test", $uid);
        $title = "内测邀请通知";
        $uuid = uuid();
        $to_uid = $uid;
        try {
            $titles = [
                "👑送你一个免费的超级会员~",
                "❤️恭喜你成为“心动考验”内测用户",
            ];
            $contents = [
                "参与内测，抢先体验新玩法“心动考验”！\n通过[①接唱]、[②问答]的方式设置考验，对你发出邀请的人需通过考验。完成两项设置，即可获得7天超级会员体验资格！",
                "新玩法抢先体验！通过[①接唱]、[②问答]的方式设置考验，对你发出邀请的人需通过考验。完成两项设置，还可获得7天超级会员👑体验资格！",
            ];
            // 发送系统消息
            NoticeModel::create([
                'type' => 5,
                'uid' => $uid,
                'create_at' => time(),
                'title' => $titles[rand(0, 1)],
                'content' => $contents[rand(0, 1)],
                'tab_content' => '参与内测',
                'tab_url' => '/invite-test/pages/my-test/my-test',
                'tab_action' => 'test',
            ]);

            $nls = new NoticeLogService();


            $content = "[爱心]恭喜你获得新玩法【心动考验】内测资格（每日限量100人）\n\n完成两项“心动考验”设置，可获得7天超级会员👑体验资格！\n同时你的交友卡片将被优先展示并拥有专属显示样式✨，增加三倍受邀率。\n心动考验：通过[接唱]，[问答]的方式设置考验，对你发出邀请的人需通过考验。";

            $openid = Openid::ofPublic($uid)->value('openid');
            $page = "pages/starter/starter?log_type=notice&log_id={$uuid}&launch_type=free&url=%2Fpages%2Fsystem-notice%2Fsystem-notice%3Faction%3Dtest";
            // 客服消息
            $kfcontent = "{$content}\n\n<a data-miniprogram-appid='wx4c1722a4055bd229' data-miniprogram-path='{$page}'>点此体验新玩法</a>";
            $response = Kfaccount::text([
                'touser' => $openid,
                'content' => $kfcontent,
                'kf_account' => 'notice@admin',
            ]);
            if ($response['code'] == 0) {
                $nls->record($uid, $title, "公众号客服消息", $uuid, 1, $kfcontent);

                return true;
            }

            // 模板消息
            $user = UserModel::findOrFail($uid);
            $public_id = \config('wechat.fpdx.public_id');
            $template_id = "QsgWayipj_3nUDIcLOWqPZI3Rm1D9SYCELfNeF-PgWs";
            $data = [
                'first' => [
                    'value' => "❤恭️喜你获得全新功能「心动考验」内测资格（每天限量500人)每日前100名设置考验的用户还可获得7天超级会员体验资格！！\n",
                ],
                'keyword1' => [
                    'value' => $user->nickname,
                ],
                'keyword2' => [
                    'value' => "即日起30日内有效",
                ],
                'keyword3' => [
                    'value' => "设置考验测ta真心：考验设置后，对你发出心动邀请需通过考验！同时你的交友卡片将拥有专属心动邀请显示样式，增加3倍受邀率！",
                ],
                'keyword4' => [
                    'value' => date('Y-m-d'),
                ],
                'remark' => [
                    'value' => "点此参与内测",
                    "color" => "#FF7E98",
                ],
            ];
            $core = new Core();
            $result = $core->template($user->uid, $template_id, $public_id, $page, $data);
            if ($result) {
                $nls->record($user->uid, $title, "公众号模板消息", $uuid, 1, $data);

                return true;
            }
        } catch (\Exception $exception) {
            app('sentry')->captureException($exception);
        }
        $nls->record($to_uid, $title, "发送失败", $uuid);

        return false;
    }
}
