<?php

namespace App\Services\User\Notifications;

use App\Services\NoticeService\Channels\TemplateChannel;
use App\Services\NoticeService\Notification;

class OldUserToVipNotification extends Notification
{


    protected $title = "老用户召回-激活会员通知";


    public function __construct($to_uid)
    {
        parent::__construct($to_uid);
    }


    /**
     * 是否订阅
     * @return bool
     */
    public function subscribe(): bool
    {
        return true;
    }


    /**
     * 节流阀
     * @return bool
     */
    public function throttle(): bool
    {
        if (
            \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', $this->title)
            ->where('uid', $this->to_uid)
            ->first()
        ) {
            return false;
        }

        return true;
    }


    /**
     * 通知渠道
     * @return array
     */
    public function via(): array
    {
        return [
            $this->toTemplate(),
        ];
    }

    /**
     * 模版消息
     * @return TemplateChannel
     */
    public function toTemplate()
    {
        $templates = [
            [
                'template_id' => 'alWufNQA9zhVec2RynEUqEUIl2l7TJZjvI6K0YZLzJM',
                'data' => [
                    'first' => [
                        'value' => "恭喜你，获得免费激活会员机会！点击此消息激活会员\n",
                    ],
                    'keyword1' => [
                        'value' => $this->to_uid,
                    ],
                    'keyword2' => [
                        'value' => date('Y-m-d'),
                    ],
                    'remark' => [
                        'value' => "\n激活成功后你将获得以下会员特权：\n1.增加专属活动匹配成功率；\n2.领取每日专属小花；\n3.每日精选日报；\n4.及时心动消息通知",
                    ],
                ],
            ],
            [
                'template_id' => 'XUDNpO1NiDufZi5z7MyY4LaG_gvpmMw8QmehHow-ptY',
                'data' => [
                    'first' => [
                        'value' => "恭喜你，获得免费激活会员机会！点击此消息激活会员\n",
                    ],
                    'keyword1' => [
                        'value' => "会员激活",
                    ],
                    'keyword2' => [
                        'value' => date('Y-m-d'),
                    ],
                    'keyword3' => [
                        'value' => "你的会员ID：{$this->to_uid}",
                    ],
                    'remark' => [
                        'value' => "\n激活成功后你将获得以下会员特权：\n1.增加专属活动匹配成功率；\n2.领取每日专属小花；\n3.每日精选日报；\n4.及时心动消息通知",
                    ],
                ],
            ],
        ];

        $rand = rand(0, count($templates) - 1);
        $templateId = $templates[$rand]['template_id'];
        $data = $templates[$rand]['data'];

        return new TemplateChannel($this->to_uid, $templateId, $data);
    }

    /**
     * 跳转地址
     * @return string
     */
    protected function getPagePath()
    {
        return "pages/vip/vip?type=normal";
    }
}
