<?php

namespace App\Http\Controllers\Goodnight;

use App\Services\Goodnight\NoticeService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Miniprogram\Auth;
use App\Models\Goodnight\CommentModel;
use App\Models\Goodnight\VoiceModel;

class CommentController extends Controller
{


    /**
     * 语音评论列表
     * @param  Request  $request
     * @param $voice_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request, $voice_id)
    {
        $voice = VoiceModel::findOrFail($voice_id);
        $builder = CommentModel::where('voice_id', $voice_id)->where('deleted_at', 0);
        $comments = $builder->orderBy('id', 'desc')->paginate($request->get('per_page', 20));
        foreach ($comments as $comment) {
            $comment->user;
        }
        return response([
            'code' => '200',
            'data' => $comments,
        ]);
    }


    /**
     * 创建评论
     * @param  Request  $request
     * @param $voice_id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function store(Request $request, $voice_id)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'content' => 'required|max:200',
        ], [
            'content.*' => '评论不得超过200个字'
        ]);

        $voice = VoiceModel::findOrFail($voice_id);
        $comment = CommentModel::create($request->merge([
            'uid' => $uid,
            'voice_id' => $voice_id
        ])->all());

        // 通知
        try {
            if ($uid != $voice->uid) {
                $ns = new NoticeService();
                $ns->voiceComment($voice->uid, $uid, $voice->id);
            }
        } catch (\Exception $exception) {
        }
        return response([
            'code' => 200,
            'data' => $comment
        ]);
    }


    /**
     * 删除留言
     * @param  Request  $request
     * @param $voice_id
     * @param $comment_id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function destroy(Request $request, $voice_id, $comment_id)
    {
        $uid = Auth::auth();
        $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
        $voice = VoiceModel::findOrFail($voice_id);
        if (!in_array($uid, [$voice->uid, $comment->uid])) {
            return response([
                'message' => '无权限'
            ], 403001);
        }
        $comment->deleted_at = time();
        $comment->save();
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }


    /**
     * 回复留言
     * @param  Request  $request
     * @param $voice_id
     * @param $comment_id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function update(Request $request, $voice_id, $comment_id)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'reply_content' => 'max:100',
        ], [
            'reply_content.max' => '回复内容不得超过100个字',
        ], [
            'reply_content' => '回复内容'
        ]);
        $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
        $voice = VoiceModel::findOrFail($voice_id);
        if ($uid != $voice->uid) {
            return response([
                'code' => 403001,
                'message' => '无权限'
            ], 403);
        }
        $reply_content = $request->get('reply_content');
        $comment->reply_content = $reply_content;
        $comment->replyed_at = time();
        $comment->save();

        // 通知
        try {
            $ns = new NoticeService();
            $ns->receiveCommentReply($comment->uid, $comment->voice_id, $uid, $reply_content);
        } catch (\Exception $e) {
            app('sentry')->captureException($e);
        }
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }
}
