<?php

namespace App\Services\User\Notifications;

use App\Models\User\UserModel;
use App\Services\NoticeService\Channels\TemplateChannel;
use App\Services\NoticeService\Notification;

class OldUserToNotCardNotification 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()
    {
        $user = UserModel::select('uid', 'nickname', 'sex')->find($this->to_uid);
        $ta = $user->sex == 2 ? "小姐姐" : "小哥哥";
        $templates = [
            [
                'template_id' => 'kFkXgPPSqT_3GfLT_PFgCJYLMsqweWrU--GWt9zv-gE',
                'data' => [
                    'first' => [
                        'value' => "嘿！亲爱的{$ta}，你的交友信息还未完成哦~\n",
                    ],
                    'keyword1' => [
                        'value' => $user->nickname,
                    ],
                    'keyword2' => [
                        'value' => date("Y-m-d"),
                    ],
                    'remark' => [
                        'value' => "\n点此完善交友信息，小象会对你进行优先推荐，让更多人看到优秀的你，帮你迈出脱单第一步！",
                    ],
                ],
            ],
            [
                'template_id' => 'vj03Lys62tcWH0BAx2bKdDbzxBnhwe4Fhs3GXqbxkTg',
                'data' => [
                    'first' => [
                        'value' => "嘿！亲爱的{$ta}，你的交友信息还未完成哦~\n",
                    ],
                    'keyword1' => [
                        'value' => "交友信息待完善",
                    ],
                    'keyword2' => [
                        'value' => date('Y-m-d'),
                    ],
                    'remark' => [
                        'value' => "\n点此完善交友信息，小象会对你进行优先推荐，让更多人看到优秀的你，帮你迈出脱单第一步！",
                    ],
                ],
            ],
        ];

        $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/my-friend-card-editor/my-friend-card-editor";
    }
}
