<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\CommentInfoModel;
use App\Models\InteractionInfo;
use App\Models\UserCareDream;
use Illuminate\Http\Request;
use App\Services\Base\ErrorCode;

class InteractionController extends Controller
{
    //    发布关于梦想的动态
    /**
     * @api {post} /api/interaction/store 新增动态
     * @apiDescription 新增动态
     * @apiGroup Interaction
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} id                 梦想ID
     * @apiParam {string} title           互动标题
     * @apiParam {array} pics[]           图片数组
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *      "status": true,
     *      "status_code": 0,
     *      "message": "",
     *      "data": ""
     *}
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     *
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'title'  => 'required',
            ],
            [
                'id.required'  => '梦想ID不能为空',
                'title.required'  => '动态标题不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
        $data = [];
        $pics = $request->pics;
        if (empty($pics) || !is_array($pics)) {
//            return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
        }else{
            foreach ($pics as $k => $pic) {
                $data['pic'.($k+1)] = $pic;
            }
        }
        $dream_id = $request->id;
        $title = $request->title;
        $data['dream_id'] = $dream_id;
        $data['title'] = $title;
        $ok = InteractionInfo::create($data);
        if ($ok) {
//            收藏梦想最新动态加一
            UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
            return $this->api('');
        }else{
            return $this->error(ErrorCode::SAVE_USER_FAILED);
        }
    }

    //    评论互动

    /**
     * @api {post} /api/interaction/comments 评论动态
     * @apiDescription 评论动态
     * @apiGroup Interaction
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} id                   动态ID不存在
     * @apiParam {string} content           内容不能为空
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *      "status": true,
     *      "status_code": 0,
     *      "message": "",
     *      "data": ""
     *}
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     *
     */
    public function comments(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'content'  => 'required',
            ],
            [
                'id.required'  => '动态ID不存在',
                'content.required'  => '内容不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

        $user = $this->getUser();
        $user_id = $user->id;
        $interaction_id = $request->id;
        $content = $request->content;
        $data = compact('user_id','interaction_id','content');
        $ok = CommentInfoModel::create($data);
        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }

    //    回复评论

    /**
     * @api {post} /api/interaction/reply 我的回复
     * @apiDescription 我的回复
     * @apiGroup Interaction
     * @apiParam {text}  content        回复内容
     * @apiParam {int} comment_id       评论ID
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "status": true,
     *   "status_code": 0,
     *   "message": "",
     *   "data": ""
     *}
     * HTTP/1.1 200 OK
     * @apiErrorExample {json} Error-Response:
     * {
     *   "status": false,
     *   "status_code": 1000,
     *   "message": "输入不正确",
     *  "data": null
     *}
     * HTTP/1.1 400 Bad Request
     */
    public function reply(Request $request)
    {
        $validator = \Validator::make($request->all(),
            [
                'id'  => 'required',
                'content'  => 'required',
            ],
            [
                'id.required'  => '评论ID不存在',
                'content.required'  => '内容不能为空',
            ]
        );

        if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);

        $user = $this->getUser();
        $data['user_id'] = $user->id;
        $data['comment_id'] = $request->id;
        $data['content'] = $request->content;

        if (!$request->content)
            return $this->error(ErrorCode::CONNET_NOT_EXIST);

        $ok =  ReplyCommentsInfo::create($data);

        if ($ok) {
            return $this->api('');
        }else{
            return $this->error(ErrorCode::OPERATION_FAILED);
        }
    }
}
