import { Router, Request, Response, NextFunction } from "express";
import { titreBase } from "../utils/define";
import { authenticate } from "../middlewares/authenticate";
import { COOKIE_TOKEN } from "../utils/constant";

export class BaseRouter {
  private _router: Router;

  constructor() {
    this._router = Router();
    this.init();
  }

  get router() {
    return this._router;
  }

  private async mainPage(req: Request, res: Response, next: NextFunction) {
    res.render("index", {
      title: `${titreBase}`,
      user: req.body.user,
    });
  }

  private async loginPage(req: Request, res: Response, next: NextFunction) {
    //res.clearCookie(cookieAuthentifiation);
    if (req.cookies[COOKIE_TOKEN]) return res.redirect("/");

    res.render("login", {
      title: `${titreBase}`,
    });
  }

  init() {
    this._router.get("/", authenticate, this.mainPage.bind(this)); // main page must be authenticate
    this._router.get("/login", this.loginPage.bind(this));
  }
}

export const baseRouter = new BaseRouter();
