<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\V1\Reviews\Resources\ReviewResource;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Product\Review\ProductReviewUpdate;
use App\Models\Ecommerce\Product\Review\Review;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ReviewController extends Controller
{
    /**
     * (*) User's reviews
     */
    public function index(Request $request): JsonResponse
    {
        return response()->rest_paginate(
            ReviewResource::collection(
                auth()->user()->reviews()->with(['product', 'product.brand'])->simplePaginate($request->perPage ?: 10)
            )
        );
    }

    /**
     * (*) Update review
     */
    public function update(ProductReviewUpdate $request, Review $review): JsonResponse
    {
        $review->update($request->validated());

        return response()->rest(message: 'Review updated successfully');
    }

    /**
     * (*) Destroy the comment from product
     */
    public function destroy(Review $review): JsonResponse
    {
        $review->delete();

        return response()->rest(message: 'Review deleted successfully');
    }
}
