// @bun
var __require = import.meta.require;

// src/libs/email/client.ts
import { createClient, Temv1alpha1 } from "@scaleway/sdk";
import { Logger } from "@contractspec/lib.logger";
var DEFAULT_FROM = {
  email: "noreply@transactional.contractspec.io",
  name: "ContractSpec"
};
var DEFAULT_TEAM_INBOX = {
  email: "contact@contractspec.io",
  name: "ContractSpec Team"
};
var DEFAULT_REGION = "fr-par";
var cachedConfig = null;
var cachedClient = null;
var apiFactory = (client) => new Temv1alpha1.API(client);
var mapRegion = (value) => {
  const normalized = value?.trim().toLowerCase();
  if (normalized === "par" || normalized === "fr-par")
    return "fr-par";
  if (normalized === "ams" || normalized === "nl-ams")
    return "nl-ams";
  if (normalized === "waw" || normalized === "pl-waw")
    return "pl-waw";
  return DEFAULT_REGION;
};
var getEmailConfig = () => {
  if (cachedConfig) {
    return { ok: true, config: cachedConfig };
  }
  const accessKey = process.env.SCALEWAY_ACCESS_KEY || process.env.SCALEWAY_ACCESS_KEY_QUEUE;
  const secretKey = process.env.SCALEWAY_SECRET_KEY || process.env.SCALEWAY_SECRET_KEY_QUEUE;
  const projectId = process.env.SCALEWAY_PROJECT_ID;
  if (!accessKey || !secretKey || !projectId) {
    return {
      ok: false,
      errorMessage: "Email service is not configured. Please contact us directly at contact@contractspec.io."
    };
  }
  const region = mapRegion(process.env.SCALEWAY_REGION);
  cachedConfig = {
    accessKey,
    secretKey,
    projectId,
    region,
    defaultZone: `${region}-1`,
    from: {
      email: process.env.SCALEWAY_EMAIL_FROM_EMAIL ?? DEFAULT_FROM.email,
      name: process.env.SCALEWAY_EMAIL_FROM_NAME ?? DEFAULT_FROM.name
    },
    teamInbox: {
      email: process.env.SCALEWAY_EMAIL_TEAM_EMAIL ?? DEFAULT_TEAM_INBOX.email,
      name: process.env.SCALEWAY_EMAIL_TEAM_NAME ?? DEFAULT_TEAM_INBOX.name
    }
  };
  return { ok: true, config: cachedConfig };
};
var getTemClient = (config) => {
  if (cachedClient) {
    return cachedClient;
  }
  const client = createClient({
    accessKey: config.accessKey,
    secretKey: config.secretKey,
    defaultProjectId: config.projectId,
    defaultRegion: config.region,
    defaultZone: config.defaultZone
  });
  cachedClient = apiFactory(client);
  return cachedClient;
};
var sendEmail = async (config, request) => {
  try {
    const client = getTemClient(config);
    await client.createEmail({
      region: config.region,
      projectId: config.projectId,
      from: config.from,
      to: request.to,
      subject: request.subject,
      text: request.text,
      html: request.html || request.text,
      additionalHeaders: request.replyTo ? [{ key: "Reply-To", value: request.replyTo }] : undefined
    });
    return { success: true };
  } catch (error) {
    new Logger().error("scaleway_tem_email_send_failed", {
      context: request.context ?? "email",
      error: error instanceof Error ? error.message : error
    });
    return {
      success: false,
      error,
      errorMessage: "Failed to send email via Scaleway."
    };
  }
};
var __internal = {
  resetCaches() {
    cachedClient = null;
    cachedConfig = null;
    apiFactory = (client) => new Temv1alpha1.API(client);
  },
  setApiFactory(factory) {
    apiFactory = factory;
  },
  setClient(client) {
    cachedClient = client;
  }
};

// src/libs/email/utils.ts
var escapeHtml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
var formatMultilineHtml = (value) => escapeHtml(value).replaceAll(`
`, "<br />");

// src/libs/email/waitlist-application.ts
"use server";
var APPLICATION_MISSING_CONFIG = "Waitlist application service is not configured. Please try again later.";
var APPLICATION_SEND_ERROR = "Failed to submit application. Please try again later or contact us directly.";
var submitWaitlistApplication = async (formData) => {
  const email = (formData.get("email") ?? "").toString().trim();
  const name = (formData.get("name") ?? "").toString().trim();
  const company = (formData.get("company") ?? "").toString().trim();
  const role = (formData.get("role") ?? "").toString().trim();
  const useCase = (formData.get("useCase") ?? "").toString().trim();
  const currentStack = (formData.get("currentStack") ?? "").toString().trim();
  const whatBuilding = (formData.get("whatBuilding") ?? "").toString().trim();
  const whatSolving = (formData.get("whatSolving") ?? "").toString().trim();
  const teamSize = (formData.get("teamSize") ?? "").toString().trim();
  const timeline = (formData.get("timeline") ?? "").toString().trim();
  const openToSessions = formData.get("openToSessions") === "on";
  const okayWithCaseStudies = formData.get("okayWithCaseStudies") === "on";
  if (!email || !email.includes("@")) {
    return {
      success: false,
      text: "Please enter a valid email address."
    };
  }
  if (!name || !whatBuilding || !whatSolving) {
    return {
      success: false,
      text: "Please fill in all required fields."
    };
  }
  const configResult = getEmailConfig();
  if (!configResult.ok || !configResult.config) {
    return {
      success: false,
      text: configResult.errorMessage ?? APPLICATION_MISSING_CONFIG
    };
  }
  const applicantText = `
You're on the list.

Thanks for applying to the ContractSpec design partner program. We're slowly onboarding design partners in waves. If your use case is a good fit, we'll reach out personally.

What happens next:
\u2022 We review applications weekly
\u2022 If selected, we'll reach out via email to schedule an intro call
\u2022 During early access, you'll get hands-on support and influence over the roadmap

In the meantime:
\u2022 Check out our docs: https://contractspec.io/docs
\u2022 Book a demo call: https://contractspec.io/contact

We're excited about the possibility of working together!

---
ContractSpec Team
https://contractspec.io
  `.trim();
  const applicantHtml = `
    <div style="font-family: sans-serif; max-width: 640px; margin: 0 auto;">
      <h1 style="color: #8b5cf6;">You're on the list.</h1>
      <p>Thanks for applying to the ContractSpec design partner program. We're slowly onboarding design partners in waves. If your use case is a good fit, we'll reach out personally.</p>
      <h2 style="color: #8b5cf6; margin-top: 24px;">What happens next:</h2>
      <ul style="line-height: 1.8;">
        <li>We review applications weekly</li>
        <li>If selected, we'll reach out via email to schedule an intro call</li>
        <li>During early access, you'll get hands-on support and influence over the roadmap</li>
      </ul>
      <h2 style="color: #8b5cf6; margin-top: 24px;">In the meantime:</h2>
      <ul style="line-height: 1.8;">
        <li>Check out our <a href="https://contractspec.io/docs" style="color: #8b5cf6;">docs</a></li>
        <li>Book a demo call: <a href="https://contractspec.io/contact" style="color: #8b5cf6;">contractspec.io/contact</a></li>
      </ul>
      <p>We're excited about the possibility of working together!</p>
      <hr style="margin: 30px 0; border: none; border-top: 1px solid #e5e7eb;" />
      <p style="color: #6b7280; font-size: 14px;">
        ContractSpec Team<br />
        <a href="https://contractspec.io" style="color: #8b5cf6;">contractspec.io</a>
      </p>
    </div>
  `;
  const applicantSend = await sendEmail(configResult.config, {
    to: [{ email }],
    subject: "You're on the ContractSpec design partner waitlist!",
    text: applicantText,
    html: applicantHtml,
    context: "waitlist-application-welcome"
  });
  if (!applicantSend.success) {
    return { success: false, text: APPLICATION_SEND_ERROR };
  }
  const preferencesText = `
Open to 1:1 product/design sessions: ${openToSessions ? "Yes" : "No"}
Okay with anonymized case studies: ${okayWithCaseStudies ? "Yes" : "No"}
  `.trim();
  const teamEmailText = `
New Design Partner Waitlist Application

Contact Information:
- Name: ${name}
- Email: ${email}
${company ? `- Company/Project: ${company}` : ""}
${role ? `- Role: ${role}` : ""}

Application Details:
- What are you building with AI today?
${whatBuilding}

- What do you hope ContractSpec will solve for you?
${whatSolving}

- Primary use case: ${useCase || "Not specified"}
- Current stack: ${currentStack || "Not specified"}
- Team Size: ${teamSize || "Not specified"}
- Timeline: ${timeline || "Not specified"}

Preferences:
- Open to 1:1 product/design sessions: ${openToSessions ? "Yes" : "No"}
- Okay with anonymized case studies: ${okayWithCaseStudies ? "Yes" : "No"}

---
Submitted via ContractSpec waitlist application form
  `.trim();
  const teamEmailHtml = `
    <div style="font-family: sans-serif; max-width: 720px; margin: 0 auto;">
      <h1 style="color: #8b5cf6;">New Design Partner Waitlist Application</h1>
      <h2 style="color: #8b5cf6; margin: 16px 0 8px;">Contact Information</h2>
      <ul style="padding-left: 16px; line-height: 1.6; margin: 0 0 16px;">
        <li>Name: ${escapeHtml(name)}</li>
        <li>Email: ${escapeHtml(email)}</li>
        ${company ? `<li>Company/Project: ${escapeHtml(company)}</li>` : ""}
        ${role ? `<li>Role: ${escapeHtml(role)}</li>` : ""}
      </ul>
      <h2 style="color: #8b5cf6; margin: 16px 0 8px;">Application Details</h2>
      <p style="margin: 0 0 8px; font-weight: 600;">What are you building with AI today?</p>
      <div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; background: #f9fafb; white-space: pre-wrap; line-height: 1.6;">
        ${formatMultilineHtml(whatBuilding)}
      </div>
      <p style="margin: 16px 0 8px; font-weight: 600;">What do you hope ContractSpec will solve for you?</p>
      <div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; background: #f9fafb; white-space: pre-wrap; line-height: 1.6;">
        ${formatMultilineHtml(whatSolving)}
      </div>
      <ul style="padding-left: 16px; line-height: 1.6; margin: 16px 0;">
        <li>Primary use case: ${escapeHtml(useCase || "Not specified")}</li>
        <li>Current stack: ${escapeHtml(currentStack || "Not specified")}</li>
        <li>Team Size: ${escapeHtml(teamSize || "Not specified")}</li>
        <li>Timeline: ${escapeHtml(timeline || "Not specified")}</li>
      </ul>
      <h2 style="color: #8b5cf6; margin: 16px 0 8px;">Preferences</h2>
      <div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; background: #f9fafb; line-height: 1.6;">
        ${formatMultilineHtml(preferencesText)}
      </div>
      <p style="color: #6b7280; font-size: 12px; margin-top: 20px;">Submitted via ContractSpec waitlist application form</p>
    </div>
  `;
  const teamSend = await sendEmail(configResult.config, {
    to: [configResult.config.teamInbox],
    subject: `New Design Partner Application: ${name} (${email})`,
    text: teamEmailText,
    html: teamEmailHtml,
    replyTo: email,
    context: "waitlist-application-team-notification"
  });
  if (!teamSend.success) {
    return { success: false, text: APPLICATION_SEND_ERROR };
  }
  return {
    success: true,
    text: "Application submitted successfully!"
  };
};
export {
  submitWaitlistApplication
};
