package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import DAO.UtilisateurDAO;
import Mapper.UtilisateurMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import metier.Utilisateur;
import org.mapstruct.factory.Mappers;

@WebServlet(name = "userinfo", urlPatterns = {"/UserInfo"})
public class UserInfo extends HttpServlet {
    UtilisateurDAO utilisateurDAO = new UtilisateurDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/UserInfo.html").forward(req, resp);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String action = request.getParameter("action");

        if ("addUser".equals(action)) {
            Utilisateur u = new Utilisateur();
            u.setName(request.getParameter("name"));
            u.setEmail(request.getParameter("email"));

            utilisateurDAO.create(u);
            PrintWriter out = response.getWriter();
            out.println("<HTML>\n<BODY>\n" +
                    "<H1>Recapitulatif des informations de Tibo</H1>\n" +
                    "<UL>\n" + " <LI>Nom: " + request.getParameter("name") +
                    "\n" + " <LI>Email: " + request.getParameter("email") + "\n" +
                    "</BODY></HTML>");
        } else if ("addSession".equals(action)) {
            int sessionId = Integer.parseInt(request.getParameter("sessionId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            utilisateurDAO.addToSession(sessionId, userId);
            response.getWriter().println("<h1> Reussi d'ajouter utilisateur " +
                    userId + " au session " + sessionId + "</h1>");
        }
    }
}

