<?php

namespace App\Controller;

use App\Entity\ReviewSource;
use App\Form\ReviewSourceType;
use App\Repository\ReviewSourceRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

/**
 * @IsGranted("ROLE_ADMIN")
 * @Route("admin/review_source")
 */
class ReviewSourceController extends AbstractController
{
    /**
     * @Route("/", name="app_review_source_index", methods={"GET"})
     */
    public function index(ReviewSourceRepository $reviewSourceRepository): Response
    {
        return $this->render('review_source/index.html.twig', [
            'review_sources' => $reviewSourceRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="app_review_source_new", methods={"GET", "POST"})
     */
    public function new(Request $request, ReviewSourceRepository $reviewSourceRepository): Response
    {
        $reviewSource = new ReviewSource();
        $form = $this->createForm(ReviewSourceType::class, $reviewSource);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $reviewSourceRepository->add($reviewSource);
            return $this->redirectToRoute('app_review_source_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('review_source/new.html.twig', [
            'review_source' => $reviewSource,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}/edit", name="app_review_source_edit", methods={"GET", "POST"})
     */
    public function edit(Request $request, ReviewSource $reviewSource, ReviewSourceRepository $reviewSourceRepository): Response
    {
        $form = $this->createForm(ReviewSourceType::class, $reviewSource);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $reviewSourceRepository->add($reviewSource);
            return $this->redirectToRoute('app_review_source_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('review_source/edit.html.twig', [
            'review_source' => $reviewSource,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="app_review_source_delete", methods={"POST"})
     */
    public function delete(Request $request, ReviewSource $reviewSource, ReviewSourceRepository $reviewSourceRepository): Response
    {
        if ($this->isCsrfTokenValid('delete'.$reviewSource->getId(), $request->request->get('_token'))) {
            $reviewSourceRepository->remove($reviewSource);
        }

        return $this->redirectToRoute('app_review_source_index', [], Response::HTTP_SEE_OTHER);
    }
}
