<?php

namespace App\Controller;

use App\Entity\Announcement;
use App\Entity\Intern;
use App\Entity\InternApplication;
use App\Entity\UserApp;
use App\Form\UserAppType;
use App\Repository\InternApplicationRepository;
use App\Repository\SkillRepository;
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;

class ProfileController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly  InternApplicationRepository $internApplicationRepository,
    ){}
    #[Route('/profile', name: 'app_profile')]
    public function profile(): Response
    {
        // Charger l'utilisateur connecté
        $user = $this->getUser();

        // Vérifiez les rôles si nécessaire
        if (!$user) {
            throw $this->createAccessDeniedException('Vous devez être connecté pour accéder à cette page.');
        }

        if ($user instanceof Intern)
        {

            $internApplications = $this->internApplicationRepository->findBy([
                'intern' => $user,
            ]);

            return $this->render('profile/index.html.twig', [
                'user' => $user,
                'applications' => $internApplications
            ]);
        }

        return $this->render('profile/index.html.twig', [
            'user' => $user,
            'applications' => []
        ]);
    }

    #[Route('profile/{id}/edit', name: 'app_profile_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, UserApp $userApp,): Response
    {
        $form = $this->createForm(UserAppType::class, $userApp);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->entityManager->flush();

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

        return $this->render('profile/edit.html.twig', [
            'user_app' => $userApp,
            'form' => $form,
        ]);
    }

    #[Route('/profile/visit/{id}', name: 'app_profile_visit')]
    public function visitProfile(int $id): Response
    {
        $candidat = $this->entityManager->getRepository(Intern::class)->find($id);

        if (!$candidat) {
            throw $this->createNotFoundException('Utilisateur non trouvé.');
        }

        return $this->render('profile/visit.html.twig', [
            'candidat' => $candidat,
        ]);
    }


    #[Route('profile/pickDegree', name: 'app_profile_pickDegree', methods: ['GET'])]
    public function pickDegree(UserApp $userApp): Response
    {
        return $this->render('degree/index.html.twig', [
            'user_app' => $userApp,
        ]);
    }

    #[Route('/profile/pickSkill', name: 'app_profile_pickSkill', methods: ['GET'])]
    public function pickSkill(UserApp $userApp): Response
    {

        return $this->render('skill/index.html.twig', [
            'user_app' => $userApp,
        ]);
    }



}