<?php

namespace App\Http\Controllers\API\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\PostCommentResource;
use App\Models\Post;
use App\Models\PostComment;
use App\Models\ReportedPostComment;
use App\Traits\HttpResponses;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class APIPostCommentController extends Controller
{
    use HttpResponses;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $attrs = $request->validate([
            'post_id' => 'required',
            'user_id' => 'required',
            'comment' => 'required',
        ]);

        $post_comment = PostComment::where('post_id', $attrs['post_id'])->where('user_id', $attrs['user_id'])->first();
        $post = Post::findOrFail($attrs['post_id']);
        PostComment::create([
            'post_id' =>  $attrs['post_id'],
            'user_id' =>  $attrs['user_id'],
            'comment' => $attrs['comment'],
            'status'  => true,
        ]);
        $post->update([
            'comment_count' => $post->comment_count + 1,
        ]);

        return $this->success(
            data: $post,
            message: 'post comment has been created successfully',
        );
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      return new PostCommentResource(PostComment::findOrFail($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $attrs = $request->validate([
            'post_id' => 'required',
            'user_id' => 'required',
            'comment' => 'required',
        ]);

        $post_comment = PostComment::find($id);
        $post_comment->update([
            'comment' => $attrs['comment'],
        ]);
        return $this->success(
            $post_comment,'Post comment '.$id.' has been updated successfully',200
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        /* $comment = PostComment::find($id);
        $this->isNotAuthorized($comment);
        return  $comment->delete(); */
    }

    private function isNotAuthorized($comment)
    {
        if (Auth::user()->id !== $comment->user_id) {
            return $this->error('', 'You are not authorized to make this request', 403);
        }
    }

    public function report($id)
    {
       $reportedPostComment  = ReportedPostComment::where('post_comment_id',$id)->where('user_id', Auth::id())->first();
       if($reportedPostComment == null){
        ReportedPostComment::create([
            'post_comment_id' => $id,
            'user_id' => Auth::id(),
        ]);
       }
    }

    public function inactive($id)
    {
        $comment = PostComment::find($id);
        $this->isNotAuthorized($comment);
        $comment->update([
            'status' => false,
        ]);

        return $this->success(
            data: null,
            message: 'Post comment has been inactivated successfully',
        );


    }
}
