<?php

namespace App\Controller;

use App\Repository\ProjectsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProjectsController extends AbstractController
{
    #[Route(path: '/projects/{name}', name: 'app_projects')]
    public function index(ProjectsRepository $projectsRepository, string $name = ''): Response
    {
        if ($name == '') {
            return $this->render(view: '@default/projects/index.html.twig', parameters: [
                'projects' => $projectsRepository->findAll(),
            ]);
        } else {
            if ($project = $projectsRepository->findOneByName(value: $name)) {
                $readMe = file_get_contents(filename: $project->getURL().'/raw/branch/master/README.md');

                // $parsedReadMe = $markdownParser->transformMarkdown(text: $readMe);

                return $this->render(view: '@default/projects/show.html.twig', parameters: [
                    'project' => $project,
                    'readme' => $readMe,
                ]);
            } else {
                throw $this->createNotFoundException();
            }
        }
    }
}
