export type ProductEdition = "mail_only" | "mail_google" | "mail_native_zimbra";
export type EditionKind = "core" | "google_addon" | "native_zimbra_addon";

export type BrandingDraft = {
  name: string;
  tagline: string;
  logoUrl: string;
  faviconUrl: string;
  logoAlt: string;
};

export const INSTALL_EDITIONS: Array<{ id: ProductEdition; title: string; description: string; kind: EditionKind }> = [
  { id: "mail_only", title: "Mail only", description: "Core webmail, contacts, signatures, filters, and attachments.", kind: "core" },
  { id: "mail_google", title: "Mail + Google", description: "Mail and Contacts with Google Calendar, Tasks, and Drive.", kind: "google_addon" },
  { id: "mail_native_zimbra", title: "Mail + Native Mail Server", description: "Core webmail with native mail server calendar and storage add-ons.", kind: "native_zimbra_addon" },
];

const EDITION_ALIASES: Record<string, ProductEdition> = {
  mail: "mail_only",
  mail_only: "mail_only",
  mailonly: "mail_only",
  webmail: "mail_only",
  webmail_only: "mail_only",
  mail_google: "mail_google",
  mail_google_addon: "mail_google",
  mail_google_calendar_drive: "mail_google",
  webmail_google: "mail_google",
  webmail_google_addon: "mail_google",
  mail_native_zimbra: "mail_native_zimbra",
  mail_native_zimbra_addon: "mail_native_zimbra",
  native_zimbra: "mail_native_zimbra",
  zimbra_native: "mail_native_zimbra",
  webmail_zimbra: "mail_native_zimbra",
  webmail_zimbra_addon: "mail_native_zimbra",
};

export function normalizeProductEdition(value?: string | null): ProductEdition {
  const token = (value || "").trim().toLowerCase().replace(/[-\s]+/g, "_");
  return EDITION_ALIASES[token] || "mail_only";
}

export function editionKind(value?: string | null): EditionKind {
  const edition = normalizeProductEdition(value);
  if (edition === "mail_only") return "core";
  if (edition === "mail_native_zimbra") return "native_zimbra_addon";
  return "google_addon";
}

export function editionAddonLabel(value?: string | null) {
  const kind = editionKind(value);
  if (kind === "google_addon") return "Google add-on";
  if (kind === "native_zimbra_addon") return "Native Mail Server add-on";
  return "Core webmail";
}

export function isCoreEdition(value?: string | null) {
  return normalizeProductEdition(value) === "mail_only";
}

export function isGoogleAddonEdition(value?: string | null) {
  return normalizeProductEdition(value) === "mail_google";
}

export function isNativeZimbraAddonEdition(value?: string | null) {
  return normalizeProductEdition(value) === "mail_native_zimbra";
}

export function buildInstallEnvSnippet(edition: ProductEdition, branding: BrandingDraft) {
  const tier = edition === "mail_google" ? "google_connect" : edition === "mail_native_zimbra" ? "zimbra_native" : "free";
  const sourceSku = "encoded_unlimited";
  const lines = [
    `PRODUCT_EDITION=${edition}`,
    `PRODUCT_TIER=${tier}`,
    `SOURCE_SKU=${sourceSku}`,
    `BRAND_NAME=${branding.name}`,
    `BRAND_TAGLINE=${branding.tagline}`,
    `BRAND_LOGO_URL=${branding.logoUrl}`,
    `BRAND_LOGO_ALT=${branding.logoAlt}`,
    `BRAND_FAVICON_URL=${branding.faviconUrl}`,
    `FRONTEND_ORIGIN=https://mail.example.com`,
    `FRONTEND_ORIGINS=https://mail.example.com`,
    `PUBLIC_IP_ADDRESS=`,
    `CANONICAL_MAIL_HOST=mail.example.com`,
    `ZIMBRA_PROVISIONING_URL=https://mailserver.example.com/service/admin/soap`,
    `ZIMBRA_ADMIN_EMAIL=admin@example.com`,
    `ZIMBRA_ADMIN_PASSWORD=`,
  ];
  if (edition === "mail_google") {
    lines.push(
      `GOOGLE_CLIENT_ID=`,
      `GOOGLE_CLIENT_SECRET=`,
      `GOOGLE_REDIRECT_URI=https://mail.example.com/calendar/oauth/callback`,
      `GOOGLE_CALENDAR_WEBHOOK_URL=https://mail.example.com/calendar/webhook`,
      `GOOGLE_CALENDAR_ID=`,
      `GOOGLE_API_KEY=`,
    );
  }
  if (edition === "mail_native_zimbra") {
    lines.push(
      `DEFAULT_ZIMBRA_SOAP_URL=https://mailserver.example.com/service/admin/soap`,
      `DEFAULT_ZIMBRA_VERIFY_SSL=false`,
    );
  }
  return lines.join("\n");
}
