<?php

namespace App\Controller;

use App\Entity\Messages;
use App\Entity\ReferralSource;
use App\Entity\User;
use App\Form\ReferralSourceFormType;
use App\Libs\Breadcrumb;
use App\Libs\Libs;
use App\Libs\NavList;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

class ReferralSourceController extends AbstractController
{
    /**
     * Variable to store unread notification messages
     *
     * @var array
     */
    private array $msgs;

    public function __construct(
        private EntityManagerInterface $entityManager,
        private array $navList = []
    ) {
        $this->navList = NavList::LIST;
        $this->navList['referral_sources'] = NavList::PRESENT_LINK;
    }

    #[Route('/list-referral-sources', name: 'app_referral_source')]
    public function listReferralSources(#[CurrentUser()] User $user): Response
    {
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
        $this->msgs = Libs::getMessages($user, $this->entityManager);
        
        $sources = $this->entityManager->getRepository(ReferralSource::class)->retrieveOrderedList();

        return $this->render(
            'internal/admin/referral_source/list-referral-sources.html.twig',
            array_merge(
                $this->navList,
                [
                    'sources' => $sources,
                    'notifications' => $this->msgs,
                    'breadcrumbs' => [
                        new Breadcrumb($this->generateUrl('app_referral_source'), 'Referral Sources')
                    ]
                ]
            )
        );
    }

    #[Route('/add-source', name: 'app_add_source')]
    public function addSource(Request $request, #[CurrentUser()] User $user): Response
    {
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
        $this->msgs = Libs::getMessages($user, $this->entityManager);
        
        $rs = new ReferralSource();
        $form = $this->createForm(ReferralSourceFormType::class, $rs);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $rs = $form->getData();

            $this->entityManager->persist($rs);
            $this->entityManager->flush();

            $this->addFlash('success', 'Referral Source added');

            return $this->redirectToRoute('app_referral_source');
        }

        return $this->render(
            'internal/admin/referral_source/add-source.html.twig',
            array_merge(
                $this->navList,
                [
                    'form' => $form,
                    'notifications' => $this->msgs,
                    'breadcrumbs' => [
                        new Breadcrumb($this->generateUrl('app_referral_source'), 'Referral Sources'),
                        new Breadcrumb($this->generateUrl('app_add_source'), 'Add Source')
                    ]
                ]
            )
        );
    }

    #[Route('/edit-source/{id}', name: 'app_edit_source')]
    public function editSource(Request $request, #[CurrentUser()] User $user, string $id): Response
    {
        $this->denyAccessUnlessGranted('ROLE_ADMIN');
        $this->msgs = Libs::getMessages($user, $this->entityManager);
        
        $rs = $this->entityManager->getRepository(ReferralSource::class)->find($id);

        $form = $this->createForm(ReferralSourceFormType::class, $rs);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $rs = $form->getData();

            $this->entityManager->flush();

            $this->addFlash('success', 'Referral Source Updated');

            return $this->redirectToRoute('app_referral_source');
        }

        return $this->render(
            'internal/admin/referral_source/edit-source.html.twig',
            array_merge(
                $this->navList,
                [
                    'form' => $form,
                    'rs' => $rs,
                    'notifications' => $this->msgs,
                    'breadcrumbs' => [
                        new Breadcrumb($this->generateUrl('app_referral_source'), 'Referral Sources'),
                        new Breadcrumb($this->generateUrl('app_edit_source', ['id' => $id]), 'Edit Source')
                    ]
                ]
            )
        );
    }
}
