// filepath: c:\Users\amaizy\Desktop\cvgen\routes\api.routes.js

const express = require("express");
const router = express.Router();
const { authenticateUser, handleCallback } = require("../controllers/linkedin");

// Route for LinkedIn authentication
router.get("/auth/linkedin", authenticateUser);

// Callback route for LinkedIn OAuth2
router.get("/auth/linkedin/callback", handleCallback);

// Traitement du formulaire d'URL LinkedIn
router.post("/me/link", express.urlencoded({ extended: true }), (req, res) => {
  if (!req.session.user) return res.redirect("/");
  req.session.user.linkedinUrl = req.body.linkedinUrl;
  res.send(`
    <p>URL LinkedIn enregistrée : <a href="${req.body.linkedinUrl}" target="_blank">${req.body.linkedinUrl}</a></p>
    <a href="/me">Retour</a>
  `);
});

module.exports = router;
