<?php


namespace App\Http\Controllers\V1;


use App\Models\User;
use App\Models\UserReport;
use App\Services\Api\MessageService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use PHPUnit\Util\Exception;

class MessageController extends Controller
{
    //消息相关内容

    private $messageService;

    public function __construct()
    {
        parent::__construct();
        $this->messageService = new MessageService();
    }

    /**
     * 获取消息首页条数、点赞条数、评论条数
     */
    public function index(){
        $data = $this->messageService->getindex();
        return  $this->success($data);
    }

    /**
     * 获取我的通知列表
     */
    public function getMyList(){
        $data = $this->messageService->getMyList();
        return  $this->success($data);
    }

    /**
     * 获取通知详细列表
     */
    public function getMyListDetail(Request $request){
        $type = $request->type;
        if(!in_array($type, [5,6,7,8])){
            return $this->error(trans('api.PARAMS_ERROR'));
        }
        $data = $this->messageService->getMyListDetail($request->id, $type);
        return  $this->success($data);
    }

    /**
     * 获取点赞消息列表
     */
    public function getLikeList(){
        DB::beginTransaction();
        try {
            $data = $this->messageService->getLikeList();
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            return $this->error($exception->getMessage());
        }
        return  $this->success($data);
    }

    /**
     * 获取评论/回复列表
     */
    public function getCommentList(){
        DB::beginTransaction();
        try {
            $data = $this->messageService->getCommentList();
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            return $this->error($exception->getMessage());
        }
        return  $this->success($data);
    }


    /**
     * 聊天举报
     */
    public function report(Request $request){
        try {
            $user_id = $request->post('user_id');
            $content = $request->post('content');
            $files = $request->post('files');
            $user = auth('api')->user();
            if(empty($user_id)){
                throw new Exception(trans('api.PARAMS_ERROR'));
            }
            $to_user = User::query()->where(['id'=>$user_id])->whereNull('deleted_at')->first();
            if(!$to_user){
                throw new Exception(trans('api.NOT_EXIST'));
            }
            $report = new UserReport();
            $report->user_id = $user->id;
            $report->type = 2;
            $report->to_id = $user_id;
            $report->content = $content;
            $report->files = !empty($files)?json_encode($files):[];
            $report->save();
            return true;
        }catch (\Exception $exception){
            return $this->error($exception->getMessage());
        }
        return  $this->success();
    }

}
