<?php

namespace Sarga\API\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Webkul\Marketplace\Repositories\ReviewRepository;
use Sarga\Shop\Repositories\VendorRepository;
use Sarga\API\Http\Resources\Core\Review;
use Webkul\RestApi\Http\Controllers\V1\V1Controller;

class ReviewMarketplace extends V1Controller
{

    public function __construct(protected VendorRepository $vendorRepository,
                                protected ReviewRepository $reviewRepository
                                )
    {
    }

    // function model()
    // {
    //     return 'Webkul\Customer\Contracts\CustomerAddress';
    // }

    public function get($seller_id){
        $review = $this->reviewRepository
        ->where('marketplace_seller_id', $seller_id)
        ->where('status', 'approved')
        ->paginate();
        
        return Review::collection($review);
    }


    public function createReview()
    {
        $request = request();
        // dd('testtt');
        $validation = Validator::make($request->all(), [
            'rating' => 'required',
            'marketplace_seller_id'=> 'required',
        ]);

        if ($validation->fails()) {
            return response()->json(['errors'=>$validation->getMessageBag()->all()],422);
        }

       if($review = $this->reviewRepository->create([
            'rating'  => $request->get('rating'),
            'comment'   => $request->get('comment'),
            'status'       => 0,
            'marketplace_seller_id'   => $request->get('marketplace_seller_id'),
            'customer_id'      => $request->user()->id,
        ])){
            return response([
                'data'    => new Review($review),
                'success' => true,
                'message' => 'Reviewed in successfully.',
            ]);
        }
        
        return response([
            'succes' => false,
            'message' => 'Unseccesfullyyyyy!!!!!.',
        ]);
    }

    public function updateReview(Request $request)
    {
       
    }
}