<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Managers\UserNoticeManager;
use App\Models\QQ\FormIdModel;
use App\Models\User\AuthKey;
use App\Models\User\Openid;
use App\Services\User\NoticeService;
use App\Services\Vendor\GeTui\ApiService;
use Illuminate\Http\Request;

class NoticeController extends Controller
{
    /**
     * 我收到的通知数量
     */
    public function noticeCnt()
    {
        $uid = Auth::auth();

        $ns = new NoticeService();
        $data = $ns->noticeCnt($uid);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 系统消息列表
     * @param  Request  $request
     * @return array
     * @deprecated UserNotice/sysnoticeList
     */
    public function sysNoticeList(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 20
        );

        $unread = $request->get('unread', 0);

        $ns = new UserNoticeManager();

        $data = $ns->systemList($uid, $pages, $unread);

        if ($pages['page'] == 1) {
            $likeMeOverView = $ns->likeMeOverView($uid);
            $inviteOverView = $ns->inviteOverView($uid);
        }

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => array(
                'likeme_overview' => $likeMeOverView ?? null,
                'invite_overview' => $inviteOverView ?? null,
                'system' => $data
            )
        ]);
    }

    /**
     * 获取最近通知
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @deprecated UserNotice/sysnoticeDesc
     */
    public function sysNoticeDesc()
    {
        $uid = Auth::auth();

        $ns = new UserNoticeManager();
        $data = $ns->systemListDesc($uid);

        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $data
        ]);
    }

    /**
     * 阅读相互心动信息
     * @param  int  $system_id
     * @return array
     * @throws \App\Exceptions\AlertException
     */
    public function readSystemNotice(int $system_id)
    {
        $uid = Auth::auth();
        $ns = new NoticeService();
        $ns->readSystemList($uid, $system_id);
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    public function notice()
    {
        $uid = Auth::auth();

        $qq = false;
        try {
            $auth = AuthKey::where(array(['uid', $uid], ['auth_type', config("qqprogram.app_id")]))->firstOrFail();
            if (
                FormIdModel::where(array(
                ['openid', $auth->auth_key],
                ['created_at', '>', time() - 6 * 86400],
                ['send_at', 0]
                ))->exists()
            ) {
                $qq = true;
            }
        } catch (\Exception $exception) {
        }

        $fpdx = Openid::isSubscribe($uid, config('wechat.fpdx.public_id'));
        $syj = Openid::isSubscribe($uid, 'gh_c94c95866ca5');

        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'qq' => $qq,
                'syj' => $syj,
                'fpdx' => $fpdx
            ]
        );
    }

    public function gtpush()
    {
        $s = new ApiService();
        $s->pushSingle();
    }
}
