<?php

namespace Modules\Rate\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Modules\Product\Services\ProductService;
use Modules\Rate\Requests\Web\RateRequest;
use Modules\Rate\Services\RateService;
use Modules\User\Services\AccountService;

class RateController extends Controller
{
    private $viewDirectory= 'web.pages.rates.';
    public function __construct(
        private AccountService $accountService,
        private ProductService $productService,
        private RateService $rateService,
    ){}

    public function rateModel($id)
    {
        $product = $this->productService->getItem($id);
        if(!$product)
        {
            return abort(404);
        }
        $action = route('web.rate');
        return view($this->viewDirectory.'rate',get_defined_vars());
    }

    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);
        return ResponseService::response([
            'status'=>200,
            'data'=>url()->previous(),
            'message'=>__('web.success')
        ]);
    }

}
