<?php

namespace App\Http\Controllers\Miniprogram;

use App\Http\Controllers\Controller;
use App\Services\Notice\ManageService;
use Illuminate\Http\Request;

class NoticeController extends Controller
{
    public function getAllProfile()
    {
        $uid = \App\Http\Controllers\Core\Auth::auth();
        $ms = new ManageService();
        $data = $ms->getNoticeByUser($uid);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    public function getNoticeByKey(string $key)
    {
        $uid = \App\Http\Controllers\Core\Auth::auth();
        $ms = new ManageService();
        $bool = $ms->getNoticeByKey($uid, $key);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'isopen' => $bool
            ]
        );
    }

    /**
     * @param  string  $key
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\ApiException
     * @deprecated Notice/GetNoticeByKeyToInt
     */
    public function getNoticeByKey2Int(string $key)
    {
        $uid = \App\Http\Controllers\Core\Auth::auth();
        $ms = new ManageService();
        $isopen = $ms->getNoticeByKey2Int($uid, $key);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'isopen' => $isopen
            ]
        );
    }

    /**
     * @param  string  $key
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \App\Exceptions\ApiException
     * @deprecated Notice/UpdateNoticeByKey
     */
    public function updateNoticeByKey(string $key, Request $request)
    {
        $is_open = $request->get('isopen', null);
        $uid = \App\Http\Controllers\Core\Auth::auth();
        $ms = new ManageService();
        $data = $ms->updateNoticeByKey($uid, $key, $is_open);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }

    public function updateNoticeByGroup(Request $request)
    {
        $uid = \App\Http\Controllers\Core\Auth::auth();
        $this->validate($request, [
            'group' => 'required',
            'isopen' => 'required|in:0,1'
        ]);
        $group = $request->post('group');
        $isopen = $request->post('isopen') ?? 0;
        $ms = new ManageService();
        $data = $ms->updateNoticeByGroup($uid, $group, $isopen);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }
}
