<?php

namespace App\Services;

use App\Helpers\EntryHelpers;
use App\Models\Entry;
use App\Models\EntryReview;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReviewsService
{

    private ?Request $request = null;
    private ?Entry $entry = null;
    private ?EntryReview $entryReview = null;

    /**
     * @throws \Throwable
     */
    public function storeReview(Request $request, Entry $entry)
    {
        // Step 1: Prepare fields.
        $this->request = $request;
        $this->entry = $entry;
        $user_id = \Auth::user()->user_id;

        $review = DB::transaction(function () use ($user_id) {

            $fields = [
                'entry_id' => $this->entry->id,
                'title' => $this->request->input('title'),
                'rating'=> $this->request->input('rating'),
                'description' => $this->request->input('description'),
                'user_id' => $user_id,
            ];

            $review = EntryReview::create($fields);

            return $review;

        });

        return $review;
    }
}
