<?php

namespace App\Http\Controllers;

use App\Exceptions\SubmissionException;
use App\Http\Requests\StoreReviewRequest;
use App\Models\Entry;
use App\Services\ReviewsService;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;

class ReviewController extends Controller
{

    public function __construct(private ReviewsService $services){}

    public function index(){

        SEOTools::setTitle('Reviews');
        return view('reviews.index');
    }


    public function store(StoreReviewRequest $request, Entry $entry)
    {
        try {
            $this->services->storeReview( $request, $entry );
            return redirect()->route('entries.show', [ 'section' => $entry->type, 'entry' => $entry ])->with('success', "Your review has been published.");
        } catch ( SubmissionException $e ) {
            return back()->withInput()->withErrors(['error' => $e->getMessage()]);
        } catch ( \Exception $e ) {
            return back()->withInput()->withErrors(['error' => 'Unknown error: '.$e->getMessage()]);
        }
    }
}
