<?php

namespace App\Services\User\Notifications;

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

class OldUserToVisitorNotification 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::find($this->to_uid);
        $ta = $user->sex == 2 ? "小哥哥" : "小姐姐";
        $number = FeedLogModel::where([
            ['partner_id', $user->partner_id],
            ['created_at', '>', strtotime("-1 month")],
        ])->count() ?: rand(1, 100);
        $templates = [
            [
                'template_id' => '5XGwNkYJuNWaWn0-GN_ROw8uVyOgJrvfdBwd8aq-wFw',
                'data' => [
                    'first' => [
                        'value' => "\n你不在的这段时间里，已经有{$number}个{$ta}看过你的交友卡片了！快来看看都有哪些人吧~\n",
                    ],
                    'keyword1' => [
                        'value' => "访客通知",
                    ],
                    'keyword2' => [
                        'value' => "已通知",
                    ],
                    'keyword3' => [
                        'value' => date('Y-m-d'),
                    ],
                    'keyword4' => [
                        'value' => "分配对象个人中心",
                    ],
                    'remark' => [
                        'value' => "\n他们之中离你最近的只有0.5km哦，来看看你附近的{$ta}吧~ ",
                    ],
                ],
            ],
            [
                'template_id' => 'ugxHaIxl5sFf2T00QZdCc0l4qELRUfEtw1HnCE7PDww',
                'data' => [
                    'first' => [
                        'value' => "\n你不在的这段时间里，已经有{$number}个{$ta}看过你的交友卡片了！快来看看都有哪些人吧~\n",
                    ],
                    'keyword1' => [
                        'value' => "访客通知",
                    ],
                    'keyword2' => [
                        'value' => "分配对象个人中心",
                    ],
                    'keyword3' => [
                        'value' => "访客通知",
                    ],
                    'keyword4' => [
                        'value' => date('Y-m-d'),
                    ],
                    'remark' => [
                        'value' => "\n他们之中离你最近的只有0.5km哦，来看看你附近的{$ta}吧~ ",
                    ],
                ],
            ],
            [
                'template_id' => 'vj03Lys62tcWH0BAx2bKdDbzxBnhwe4Fhs3GXqbxkTg',
                'data' => [
                    'first' => [
                        'value' => "你不在的这段时间里，已经有{$number}个{$ta}看过你的交友卡片了！快来看看都有哪些人吧~\n",
                    ],
                    'keyword1' => [
                        'value' => "访客通知",
                    ],
                    'keyword2' => [
                        'value' => date('Y-m-d'),
                    ],
                    'remark' => [
                        'value' => "\n他们之中离你最近的只有0.5km哦，来看看你附近的{$ta}吧~ ",
                    ],
                ],
            ],
        ];
        $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 "popularity/pages/watched/watched";
    }
}
