// src/controllers/home.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { NextFunction, Request, Response } from "express";

import { DtpController } from "../lib/controller.js";

export class HomeController extends DtpController {
  get name(): string {
    return "HomeController";
  }
  get slug(): string {
    return "home";
  }
  get route(): string {
    return "/";
  }

  constructor() {
    super();
  }

  async start(): Promise<void> {
    this.router.get("/", this.getHome.bind(this));
  }

  async getHome(
    req: Request,
    res: Response,
    next: NextFunction
  ): Promise<void> {
    try {
      if (!req.user) {
        res.status(200).json({
          success: true,
          authenticated: false,
        });
        return;
      }
      res.status(200).json({
        success: true,
        authenticated: true,
        user: {
          _id: req.user._id.toString(),
          email: req.user.email,
          displayName: req.user.displayName,
        },
      });
    } catch (error) {
      this.log.error("failed to present the home page", { error });
      return next(error);
    }
  }
}

export default HomeController;
