<?php

namespace App\Http\Controllers;

use App\Http\Requests\GradePostRequest;
use App\Models\Grade;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class GradeController extends Controller
{
    public function __construct(
        protected ServiceInterface $gradeService,
    ){}

    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        return view('grades.index', [
            'grades' => $this->gradeService->getAll(),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): View
    {
        return view('grades.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(GradePostRequest $request): RedirectResponse
    {
        return redirect()->route('grades.show', $this->gradeService->create($request->validated()));
    }

    /**
     * Display the specified resource.
     */
    public function show(Grade $grade): View
    {
        return view('grades.show', [
            'grade' =>  $grade,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Grade $grade): View
    {
        return view('grades.edit', [
            'grade' => $grade,
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(GradePostRequest $request, Grade $grade): RedirectResponse
    {
        return redirect()->route('grades.show', $this->gradeService->update($grade, $request->validated()));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Grade $grade): RedirectResponse
    {
        $this->gradeService->delete($grade);

        return redirect()->route('grades.index');
    }
}