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

// import env from "../../../config/env.js";

import { Request, Response } from "express";
// import ContactService from "../../../services/contact.js";

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

export class ContactApiControllerV1 extends DtpController {
  get name(): string {
    return "ContactApiControllerV1";
  }
  get slug(): string {
    return "contactV1";
  }
  get route(): string {
    return "/contact";
  }

  constructor() {
    super();
  }

  async start(): Promise<void> {
    const upload = this.createMulter(this.slug, {
      dest: "/tmp/",
      limits: {
        fileSize: 1024 * 1024 * 10,
      },
    });

    const limiter = this.createLimiter(
      60 * 60, // 1-hour time window
      3,
      "Too many messages sent. Please try again later.",
      (req: Request, _res: Response) => {
        if (!req.body || !req.body.email) {
          return req.ip;
        }
        return req.ip + req.body.email;
      }
    );

    this.router.post(
      "/send-email",
      limiter,
      upload.single("attachment"),
      this.postSendEmail.bind(this)
    );
  }

  async postSendEmail(_req: Request, res: Response): Promise<void> {
    try {
      // const jobs = [
      //   ContactService.sendContactUsMessage(
      //     req,
      //     env.email.contact.to,
      //     `You have received a contact message at ${env.site.name}.`
      //   ),
      //   ContactService.sendContactUsMessage(
      //     req,
      //     req.body.email,
      //     `${env.site.name} has received your message. A representative will be in touch as soon as possible.`
      //   ),
      // ];
      // await Promise.all(jobs);

      res.status(200).json({ success: true });
    } catch (error) {
      this.log.error("failed to send email message", { error });
      res.status((error as Error).statusCode || 500).json({
        success: false,
        message: (error as Error).message,
      });
    }
  }
}

export default ContactApiControllerV1;
