<?php

namespace App\Http\Controllers\Api\V1\Product;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Product\ProductCommentStore;
use App\Http\Resources\Api\V1\Common\CommentResource;
use App\Models\Common\Comment;
use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ProductCommentController extends Controller
{
    /**
     * Product's comments
     */
    public function index(Request $request, Product $product): JsonResponse
    {
        return response()->rest(
            CommentResource::collection(
                $product->comments()->with('user')->where('active', true)->get()
            )
        );
    }

    /**
     * Store
     */
    public function store(ProductCommentStore $request, Product $product): JsonResponse
    {
        $product->comments()->create([
            'comment' => $request->comment,
            'user_id' => auth()->id(),
        ]);

        return response()->rest(message: 'Comment added successfully', code: 201);
    }

    /**
     * Destroy the comment from product
     */
    public function destroy(Product $product, Comment $comment): JsonResponse
    {
        $product->comments()->where('comments.id', $comment->id)->delete();

        return response()->rest(message: 'Comment removed successfully from product', code: 204);
    }
}
