import raw from "@/data/routes.json";

export type Cta = { label: string; destination: string };
export type FormField = {
  id: string;
  label: string;
  type: string;
  required?: boolean;
  required_when?: string;
  autocomplete?: string;
  maxlength?: number;
  options?: string[];
};
export type FormDefinition = {
  submit_label: string;
  success_route: string;
  fields: FormField[];
  consents?: string[];
  consents_when_email?: string[];
};
export type SectionData = {
  h2: string;
  body?: string[];
  bullets?: string[];
  steps?: Array<{ title?: string; copy?: string; label?: string }> | string[];
  faq?: Array<{ question: string; answer: string }>;
  cta?: Cta;
  ctas?: Cta[];
  form?: string;
  component?: string;
  control?: string;
  anchor?: string;
  preset?: string;
  required_acknowledgements?: string[];
  button_copy?: string;
  fallback_copy?: string;
  fallback_cta?: Cta;
};
export type HeroData = {
  eyebrow: string;
  h1: string;
  lead?: string;
  primary_cta?: Cta;
  secondary_cta?: Cta;
};
export type PageData = {
  id: number;
  category: "commercial" | "journey" | "trust";
  route: string;
  name: string;
  indexing: string;
  access?: string;
  publication_state?: string;
  seo: { title: string; description: string; canonical: string; schema: string[] };
  hero?: HeroData;
  sections?: SectionData[];
  states?: Record<string, { eyebrow?: string; h1?: string; title?: string; copy: string; cta?: Cta }>;
  modules?: Array<{ id: string; title: string; copy: string }>;
  support_copy?: string;
};
export type VariantData = {
  id: string;
  route: string;
  variant: "A" | "B";
  hypothesis: string;
  hero_h1: string;
  hero_lead: string;
  primary_cta: string;
  destination: string;
  primary_metric: string;
};

type CommandData = {
  pages: PageData[];
  ab_variants: VariantData[];
  forms: Record<string, FormDefinition>;
  diagnosis: {
    disclaimer: string;
    scale: Record<string, string>;
    questions: Array<{ id: string; dimension: string; copy: string }>;
    results: Record<string, { title: string; summary: string; exercise: string; cta: string; destination: string }>;
  };
  consents: Record<string, { required: boolean; copy: string }>;
  ticket_model: { publication_state: string; gate: string; tiers: Array<{ id: string; label: string; draft_inclusions: string[] }> };
};

export const comando = raw as CommandData;

export function pageForRoute(route: string) {
  return comando.pages.find((page) => page.route === route);
}

export function variantFor(route: string, requested?: string) {
  const variants = comando.ab_variants.filter((item) => item.route === route);
  if (!variants.length) return undefined;
  const key = requested?.toUpperCase() === "B" ? "B" : "A";
  return variants.find((item) => item.variant === key) ?? variants[0];
}

export function publicBaseUrl() {
  return process.env.NEXT_PUBLIC_SITE_URL || "https://comando-imersao.atanazio333.chatgpt.site";
}
