/* Shared utilities for MeetingMind across pages */
const STORAGE_KEY = "meetingmind:meetings:v1";
const PREFS_KEY = "meetingmind:prefs:v1";
const AUTH_KEY = "meetingmind:auth:v1";

function loadMeetings() {
  try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]"); }
  catch { return []; }
}
function saveMeetings(items) {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(items));
}
function loadPrefs() {
  try { return JSON.parse(localStorage.getItem(PREFS_KEY) || "{}"); }
  catch { return {}; }
}
function savePrefs(p) {
  localStorage.setItem(PREFS_KEY, JSON.stringify(p));
}
function loadAuth() {
  try { return JSON.parse(localStorage.getItem(AUTH_KEY) || "null"); }
  catch { return null; }
}
function saveAuth(a) {
  if (a) localStorage.setItem(AUTH_KEY, JSON.stringify(a));
  else localStorage.removeItem(AUTH_KEY);
}

function applyTheme(theme) {
  document.documentElement.setAttribute("data-theme", theme || "light");
}
function applyDensity(d) {
  document.documentElement.setAttribute("data-density", d || "cozy");
}
function applyAccent(hue) {
  if (hue == null) return;
  document.documentElement.style.setProperty("--accent", `oklch(0.62 0.17 ${hue})`);
  document.documentElement.style.setProperty("--accent-hover", `oklch(0.56 0.18 ${hue})`);
  document.documentElement.style.setProperty("--accent-soft", `oklch(0.96 0.03 ${hue})`);
}

/* i18n — PT default, EN toggle */
const STRINGS = {
  pt: {
    nav_features: "Recursos",
    nav_pricing: "Preços",
    nav_faq: "FAQ",
    nav_signin: "Entrar",
    nav_app: "Abrir app",
    nav_dashboard: "Dashboard",
    nav_logout: "Sair",
    hero_eyebrow: "Em alta agora",
    hero_title: "Reuniões viram decisões.\nNão mais um drive cheio de notas.",
    hero_sub: "Cole a transcrição da sua reunião. Em segundos você recebe resumo, decisões, action items com responsáveis e um e-mail de follow-up pronto pra enviar.",
    hero_cta: "Testar grátis",
    hero_cta2: "Ver demo",
    hero_meta: "3 reuniões grátis · sem cartão · funciona em PT e EN",
    feat_title: "Tudo que você quer da reunião,\nzero do que você não quer.",
    feat_sub: "Transcrição entra. Decisões saem. Sem treinamento, sem prompt, sem fricção.",
    how_title: "Como funciona",
    how_sub: "Três passos. Menos de 30 segundos.",
    pricing_title: "Preços simples",
    pricing_sub: "Comece grátis. Pague quando precisar de mais.",
    faq_title: "Perguntas frequentes",
    cta_title: "Pronto pra parar de digitar ata?",
    cta_sub: "Sua próxima reunião pode terminar com um follow-up no inbox de todo mundo.",
    footer_made: "Feito com café em São Paulo",
  },
  en: {
    nav_features: "Features",
    nav_pricing: "Pricing",
    nav_faq: "FAQ",
    nav_signin: "Sign in",
    nav_app: "Open app",
    nav_dashboard: "Dashboard",
    nav_logout: "Sign out",
    hero_eyebrow: "Trending now",
    hero_title: "Turn meetings into decisions.\nNot another folder of notes.",
    hero_sub: "Paste your meeting transcript. In seconds you get a summary, decisions, owned action items, and a follow-up email ready to send.",
    hero_cta: "Try it free",
    hero_cta2: "Watch demo",
    hero_meta: "3 free meetings · no card · works in EN and PT",
    feat_title: "Everything you want from a meeting,\nnone of what you don't.",
    feat_sub: "Transcript in. Decisions out. No training, no prompts, no friction.",
    how_title: "How it works",
    how_sub: "Three steps. Under 30 seconds.",
    pricing_title: "Simple pricing",
    pricing_sub: "Start free. Pay when you need more.",
    faq_title: "Frequently asked",
    cta_title: "Ready to stop writing meeting notes?",
    cta_sub: "Your next meeting can end with a follow-up in everyone's inbox.",
    footer_made: "Made with coffee in São Paulo",
  },
};

function useLang() {
  const [lang, setLangState] = React.useState(() => loadPrefs().lang || "pt");
  const setLang = (l) => {
    setLangState(l);
    savePrefs({ ...loadPrefs(), lang: l });
  };
  return [lang, setLang];
}
function t(lang, key) { return (STRINGS[lang] && STRINGS[lang][key]) || STRINGS.pt[key] || key; }

/* Format helpers */
function timeAgo(iso, lang = "pt") {
  const d = new Date(iso); const now = new Date();
  const diff = (now - d) / 1000;
  const map = lang === "en"
    ? [[60, "s"], [3600, "min"], [86400, "h"], [604800, "d"], [2592000, "w"], [Infinity, "mo"]]
    : [[60, "s"], [3600, "min"], [86400, "h"], [604800, "d"], [2592000, "sem"], [Infinity, "mês"]];
  if (diff < 60) return lang === "en" ? "just now" : "agora";
  if (diff < 3600) return `${Math.floor(diff / 60)} min`;
  if (diff < 86400) return `${Math.floor(diff / 3600)} h`;
  if (diff < 604800) return `${Math.floor(diff / 86400)} d`;
  return d.toLocaleDateString(lang === "en" ? "en-US" : "pt-BR", { day: "numeric", month: "short" });
}

function uid() { return Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4); }

/* Tiny inline icon set (stroke icons) */
function Icon({ name, size = 16, ...rest }) {
  const props = {
    width: size, height: size, viewBox: "0 0 24 24", fill: "none",
    stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round",
    ...rest,
  };
  switch (name) {
    case "arrow-right": return <svg {...props}><path d="M5 12h14M13 6l6 6-6 6"/></svg>;
    case "check": return <svg {...props}><path d="M20 6 9 17l-5-5"/></svg>;
    case "sparkles": return <svg {...props}><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></svg>;
    case "play": return <svg {...props}><path d="M8 5v14l11-7z"/></svg>;
    case "menu": return <svg {...props}><path d="M3 6h18M3 12h18M3 18h18"/></svg>;
    case "sun": return <svg {...props}><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg>;
    case "moon": return <svg {...props}><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>;
    case "doc": return <svg {...props}><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><path d="M14 2v6h6M9 13h6M9 17h6"/></svg>;
    case "list": return <svg {...props}><path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01"/></svg>;
    case "users": return <svg {...props}><path d="M16 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="8.5" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75"/></svg>;
    case "mail": return <svg {...props}><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><path d="m22 6-10 7L2 6"/></svg>;
    case "shield": return <svg {...props}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>;
    case "zap": return <svg {...props}><path d="M13 2 3 14h9l-1 8 10-12h-9l1-8z"/></svg>;
    case "globe": return <svg {...props}><circle cx="12" cy="12" r="10"/><path d="M2 12h20M12 2a15.3 15.3 0 0 1 0 20M12 2a15.3 15.3 0 0 0 0 20"/></svg>;
    case "trash": return <svg {...props}><path d="M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/></svg>;
    case "copy": return <svg {...props}><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>;
    case "plus": return <svg {...props}><path d="M12 5v14M5 12h14"/></svg>;
    case "search": return <svg {...props}><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>;
    case "logout": return <svg {...props}><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4M16 17l5-5-5-5M21 12H9"/></svg>;
    case "calendar": return <svg {...props}><rect x="3" y="4" width="18" height="18" rx="2"/><path d="M16 2v4M8 2v4M3 10h18"/></svg>;
    case "chevron-down": return <svg {...props}><path d="m6 9 6 6 6-6"/></svg>;
    case "external": return <svg {...props}><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><path d="M15 3h6v6M10 14 21 3"/></svg>;
    default: return null;
  }
}

window.Icon = Icon;
window.MM = {
  loadMeetings, saveMeetings, loadPrefs, savePrefs, loadAuth, saveAuth,
  applyTheme, applyDensity, applyAccent,
  STRINGS, t, useLang, timeAgo, uid,
};
