<?php

namespace Modules\Rate\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Product\Models\Resources\ProductResource;
use Modules\Product\Requests\Api\FavoriteRequest;
use Modules\Product\Services\ProductService;
use Modules\Rate\Models\Resources\RateResource;
use Modules\Rate\Requests\Api\RateRequest;
use Modules\Rate\Services\RateService;
use Modules\User\Services\AccountService;

class RateController extends Controller
{
    private int $paginate = 12;

    public function __construct(
        private AccountService $accountService,
        private RateService $rateService,
    ){}

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function rates($id):JsonResponse
    {
        $rates = $this->rateService->get([
            'product_id' => $id
        ]);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=> RateResource::collection($rates)
        ]);
    }

    public function rate(RateRequest $request)
    {
        $account = $this->accountService->getAccount();
        $data = $request->validated();
        $data['user_id'] = $account['user']->id;
        $data['user_status'] = $account['status'];

        $this->rateService->create($data);
        $rates = $this->rateService->get([
            'product_id'=>$request->product_id
        ]);

        return ResponseService::response([
            'status'=>200,
            'message'=>__('web.success'),
            'data'=>RateResource::collection($rates)
        ]);
    }

}
