import type { Plan, PlanFormValues, PlanLimitationsMeta } from "@/types/plan";

export function planToForm(plan?: Plan): PlanFormValues {
  const lim = (plan?.limitations ?? {}) as PlanLimitationsMeta;
  const idealFor = lim.idealFor ?? [];

  return {
    name: plan?.name ?? "",
    slug: plan?.slug ?? "",
    shortDescription: plan?.shortDescription ?? plan?.description ?? "",
    fullDescription: plan?.fullDescription ?? plan?.description ?? "",
    monthlyPrice: plan?.monthlyPrice ?? 0,
    yearlyPrice: plan?.yearlyPrice ?? 0,
    compareAtMonthly: plan?.compareAtMonthly ?? "",
    compareAtYearly: plan?.compareAtYearly ?? "",
    setupFee: plan?.setupFee ?? 0,
    featured: plan?.featured ?? false,
    badge: plan?.badge ?? "",
    buttonText: plan?.buttonText ?? "Quero meu app",
    buttonLink: plan?.buttonLink ?? "",
    icon: plan?.icon ?? "",
    active: plan?.active ?? true,
    limitsApps: plan?.limits?.apps ?? 1,
    limitsUsers: plan?.limits?.users ?? 3,
    limitsSupport: plan?.limits?.support ?? "E-mail",
    seoTitle: plan?.seoTitle ?? "",
    seoDescription: plan?.seoDescription ?? "",
    planFeatures:
      plan?.planFeatures?.map((f) => ({
        feature: f.feature,
        highlight: f.highlight,
        icon: f.icon ?? "",
      })) ??
      plan?.features?.map((f) => ({ feature: f, highlight: false, icon: "" })) ??
      [{ feature: "", highlight: false, icon: "" }],
    planningEmoji: lim.emoji ?? "",
    planningPriceFrom: lim.planningPriceFrom ?? false,
    screenEstimate: lim.screenEstimate ?? "",
    structuralDescription: lim.structuralDescription ?? "",
    planningNotice: lim.planningNotice ?? "",
    planningTimeline: lim.planningTimeline ?? "",
    idealForText: idealFor.join("\n"),
  };
}

/** Mescla o rascunho do formulário no plano para preview idêntico ao site. */
export function formValuesToPreviewPlan(base: Plan, values: PlanFormValues): Plan {
  const idealFor = (values.idealForText ?? "")
    .split("\n")
    .map((s) => s.trim())
    .filter(Boolean);

  const limitations: PlanLimitationsMeta = {
    emoji: values.planningEmoji || undefined,
    planningPrice: values.monthlyPrice,
    planningPriceFrom: values.planningPriceFrom,
    screenEstimate: values.screenEstimate || undefined,
    structuralDescription: values.structuralDescription || undefined,
    planningNotice: values.planningNotice || undefined,
    planningTimeline: values.planningTimeline || undefined,
    idealFor: idealFor.length ? idealFor : undefined,
  };

  const planFeatures = values.planFeatures
    .filter((f) => f.feature.trim())
    .map((f, i) => ({
      feature: f.feature.trim(),
      highlight: f.highlight,
      icon: f.icon || null,
      sortOrder: i + 1,
    }));

  return {
    ...base,
    name: values.name,
    slug: values.slug,
    shortDescription: values.shortDescription,
    fullDescription: values.fullDescription,
    description: values.fullDescription || values.shortDescription,
    monthlyPrice: values.monthlyPrice,
    yearlyPrice: values.yearlyPrice,
    compareAtMonthly:
      values.compareAtMonthly === "" ? null : Number(values.compareAtMonthly),
    compareAtYearly:
      values.compareAtYearly === "" ? null : Number(values.compareAtYearly),
    setupFee: values.setupFee,
    featured: values.featured,
    badge: values.badge || null,
    buttonText: values.buttonText || null,
    buttonLink: values.buttonLink || null,
    icon: values.icon || null,
    active: values.active,
    features: planFeatures.map((f) => f.feature),
    planFeatures,
    limits: {
      apps: values.limitsApps,
      users: values.limitsUsers,
      support: values.limitsSupport,
    },
    limitations,
    seoTitle: values.seoTitle || null,
    seoDescription: values.seoDescription || null,
  };
}
