// Welcome screen — shown once after first sign-up. Plants a small set of
// example rows so the rest of the app isn't empty, then lands on Today.
(function () {
  const { useState, useEffect } = React;
  const S = window.KomonichiState;

  // Sample data lives on the client because it's not security-sensitive and
  // it keeps the service surface minimal.
  // Coords are fractions of the brain-map world (0..1).
  const SAMPLE_AREAS = [
    { name: "Body",  x: 0.30, y: 0.32, tone: "moss",      note: "movement, breath, sleep" },
    { name: "Mind",  x: 0.68, y: 0.36, tone: "indigo",    note: "reading, writing, study" },
    { name: "Craft", x: 0.50, y: 0.68, tone: "persimmon", note: "the work itself" },
  ];
  const SAMPLE_OBJECTIVES = [
    { period: "yearly",    text: "Cultivate a daily practice." },
    { period: "quarterly", text: "Read four books slowly." },
    { period: "monthly",   text: "Walk every morning." },
    { period: "weekly",    text: "Write 500 quiet words." },
  ];
  const SAMPLE_TASKS = [
    { title: "Sit for ten minutes.",       estimate_min: 10, areaName: "Body"  },
    { title: "Open the book on the desk.", estimate_min: 25, areaName: "Mind"  },
    { title: "Sketch one small idea.",     estimate_min: 20, areaName: "Craft" },
  ];

  async function seedExamples() {
    const areaIds = {};
    for (const a of SAMPLE_AREAS) {
      const created = await S.actions.addArea({ name: a.name, x: a.x, y: a.y, tone: a.tone, note: a.note });
      areaIds[a.name] = created.area_id || created.id;
    }
    for (const o of SAMPLE_OBJECTIVES) {
      await S.actions.addObjective(o.text, o.period, null, { seeded: true });
    }
    for (const t of SAMPLE_TASKS) {
      await S.actions.addTask({
        title: t.title,
        estimate_min: t.estimate_min,
        area_id: areaIds[t.areaName] || null,
      });
    }
  }

  function WelcomeScreen({ onReady }) {
    const [busy, setBusy] = useState(false);
    const [err, setErr]   = useState(null);

    async function ready() {
      setBusy(true); setErr(null);
      try {
        await seedExamples();
        onReady();
      } catch (e) {
        setErr(e.message);
        setBusy(false);
      }
    }

    return React.createElement("div", { className: "welcome-wrap fade-in" },
      React.createElement("div", { className: "welcome-card" },
        React.createElement("div", { className: "enso", style: { margin: "0 auto 24px" } }),
        React.createElement("div", { className: "label", style: { textAlign: "center" } }, "to begin"),
        React.createElement("h1", {
          className: "display",
          style: { fontSize: 56, lineHeight: 1.05, textAlign: "center", margin: "10px 0 22px" },
        },
          React.createElement("em", null, "ground"), " yourself."
        ),
        React.createElement("p", { className: "welcome-body" },
          "Komonichi is a quiet place for the practice of attention. Not a productivity tool — a small wooden bench. Sit with what matters: the body, the mind, the craft. Make objectives you cannot edit, tasks you can let go of, sessions you can simply count. Walk away when you've done enough."
        ),
        React.createElement("blockquote", { className: "welcome-quote" },
          React.createElement("span", { className: "welcome-quote-text" },
            "“The journey of a thousand miles begins beneath one's feet.”"),
          React.createElement("span", { className: "welcome-quote-attr" }, "— 老子 Laozi")
        ),
        err ? React.createElement("div", { className: "auth-err", style: { margin: "0 auto", maxWidth: 380 } }, err) : null,
        React.createElement("div", { style: { textAlign: "center", marginTop: 32 } },
          React.createElement("button", {
            className: "btn warm",
            disabled: busy,
            onClick: ready,
            style: { padding: "14px 28px", fontSize: 11, letterSpacing: "0.28em" },
          }, busy ? "preparing…" : "i'm ready")
        )
      )
    );
  }

  window.KomonichiWelcome = { WelcomeScreen };
})();
