"use server";

import { sendEmail } from "../lib/mail/mailer";
import {
  getInquiryEmailHtml,
  getConfirmationEmailHtml,
} from "../components/ContactForm/EmailTemplates";

export async function sendContactInquiry(data: {
  name: string;
  email: string;
  companyName: string;
  projectType: string;
  message: string;
  isFreeText: boolean;
  config?: any;
}) {
  try {
    // 1. Send Inquiry to Marc
    const inquiryResult = await sendEmail({
      subject: `[PROJEKT] ${data.isFreeText ? "DIREKTANFRAGE" : "KONFIGURATION"}: ${data.companyName || data.name}`,
      html: getInquiryEmailHtml(data),
      replyTo: data.email,
    });

    if (!inquiryResult.success) {
      throw new Error(inquiryResult.error);
    }

    // 2. Send Confirmation to Customer
    await sendEmail({
      to: data.email,
      subject: `Kopie deiner Anfrage: ${data.companyName || "mintel.me"}`,
      html: getConfirmationEmailHtml(data),
    });

    return { success: true };
  } catch (error) {
    console.error("Server Action Error:", error);
    return {
      success: false,
      error:
        error instanceof Error
          ? error.message
          : "Unknown error during submission",
    };
  }
}
