/* ============================================================
   AI engine (scripted, deterministic) + Command Palette + Assistant
   ============================================================ */

const AI_KB = [
  {
    match: ["late", "submission", "penalty", "deadline", "extension"],
    text: t("The Late & Missing Coursework policy applies a 5% penalty per calendar day late, capped at 3 days — after which the submission scores zero unless a formal extension was approved in advance. Extensions require Academic Director sign-off and are normally granted only on documented medical or exceptional grounds."),
    sources: [{ doc: t("Academic Integrity & Submission Policy"), ref: t("§4.2 Late penalties"), v: "v2.1" }, { doc: t("IA/EE Handbook"), ref: t("Deadlines & extensions"), v: "v1.4" }],
    actions: [t("List all late submissions this week"), t("Draft an extension-approval note")],
  },
  {
    match: ["risk", "why", "flagged", "explain", "dilnoza", "s-101"],
    text: t("Dilnoza R. (DP2, Class A) scores 86 — Critical. The score is driven by four signals: attendance at 81% (22 pts), an active Red Report now in week 2 (24 pts), two HL subjects flagged weak — Chemistry and Mathematics AA (18 pts), and a Chemistry IA submitted 4 days late (14 pts). The fastest-moving risk is behaviour; her last two data points trend downward in Mathematics."),
    sources: [{ doc: t("At-Risk Radar — scoring model"), ref: t("Weighted factors"), v: "live" }, { doc: t("Student record S-101"), ref: t("Term 3 timeline"), v: "live" }],
    actions: [t("Suggest next actions for Dilnoza R."), t("Draft a homeroom check-in message")],
    draftHint: false,
  },
  {
    match: ["next action", "suggest", "intervention", "what should"],
    text: t("Suggested next actions, in priority order:\n1. Convene the week-2 Red Report review with the homeroom teacher before Friday.\n2. Move Mathematics AA to Tier 2 academic support — two consecutive downward data points.\n3. Confirm the Chemistry IA penalty has been logged and the student notified.\n4. Schedule a parent check-in; attendance has had no parent contact on the last unexcused absence.\nThese are drafts for your review — no action is taken until you approve."),
    sources: [{ doc: t("Pastoral Intervention Framework"), ref: t("Tier 2 triggers"), v: "v3.0" }],
    actions: [t("Draft the parent check-in message"), t("Open Dilnoza R. profile")],
    draftHint: true,
  },
  {
    match: ["draft", "report comment", "comment", "write"],
    text: t("Draft report comment — Dilnoza R., Mathematics AA HL:\n\n\"Dilnoza has the analytical ability to succeed at HL, but recent assessment data shows calculus topics are not yet secure, and this has affected her predicted grade. With consistent attendance and targeted support on Paper 2 technique, a return to a 5 is realistic this term. I encourage her to use the Tuesday clinic.\"\n\nThis is a draft. Review, edit, and approve before it is added to the report."),
    sources: [{ doc: t("Report Writing Guidance"), ref: t("Tone & structure"), v: "v1.2" }],
    actions: [t("Make it more concise"), t("Regenerate for a parent audience")],
    draftHint: true,
  },
  {
    match: ["attendance", "absence", "present"],
    text: t("Attendance is recorded per lesson using P (present), A (absent), L (late), E (excused) and S (school activity). The whole-school average this term is ") + (window.DB ? window.DB.stats.avgAtt : 90) + t("%. Any student falling below 90% over a rolling two-week window is automatically surfaced on the At-Risk Radar and weighted into their score."),
    sources: [{ doc: t("Attendance & Punctuality Policy"), ref: t("Codes & thresholds"), v: "v2.0" }],
    actions: [t("Show students below 85% attendance"), t("Open the weekly register")],
  },
  {
    match: ["compliance", "training", "ib", "verification", "policy review"],
    text: t("Compliance readiness is currently amber. 9 of 12 policies are within their review date (2 due this term, 1 overdue). IB subject-training coverage shows 2 teachers expiring and 2 confirmed gaps — Economics HL and one Mathematics AA HL section. Coursework completeness sits at ") + (window.DB ? window.DB.stats.iaComplete : 85) + t("%. I can open the Compliance Cockpit to the specific gaps."),
    sources: [{ doc: t("IB Programme Standards & Practices"), ref: t("Staffing & resources"), v: "2020" }, { doc: t("Staff Training Register"), ref: t("Live"), v: "live" }],
    actions: [t("Open Compliance Cockpit"), t("List the training gaps")],
  },
];

window.AI = {
  respond(prompt) {
    const p = (prompt || "").toLowerCase();
    let best = null, bestScore = 0;
    for (const e of AI_KB) {
      const s = e.match.reduce((a, m) => a + (p.includes(m) ? 1 : 0), 0);
      if (s > bestScore) { bestScore = s; best = e; }
    }
    if (!best) {
      return {
        text: t("I can help with student risk, behaviour and pastoral workflows, IB compliance, attendance, coursework deadlines, and drafting report comments or messages — always citing the school's own documents. Try one of the suggestions below."),
        sources: [], actions: [t("Summarise this student's risk"), t("Find the late-submission policy"), t("Draft a report comment")],
      };
    }
    return best;
  },
};

/* ---------- Command Palette ---------- */
function CommandPalette({ role, onClose, onAssistant }) {
  const [q, setQ] = React.useState("");
  const inputRef = React.useRef(null);
  React.useEffect(() => { inputRef.current?.focus(); }, []);

  const screens = Object.keys(SCREEN_TITLES).filter((k) => k !== "student")
    .filter((k) => role === "director" || !["ilp", "compliance", "staff"].includes(k));
  const ql = q.toLowerCase();

  const screenHits = screens.filter((k) => SCREEN_TITLES[k].toLowerCase().includes(ql)).slice(0, 6);
  const studentHits = window.DB.STUDENTS.filter((s) => s.name.toLowerCase().includes(ql) || s.id.toLowerCase().includes(ql)).slice(0, 6);
  const aiActions = [
    t("Summarise the school's risk position"), t("Find the late-submission policy"),
    t("Draft a report comment"), t("List students below 85% attendance"),
  ].filter((a) => a.toLowerCase().includes(ql)).slice(0, 4);

  return (
    <div className="cmd-overlay" onMouseDown={onClose}>
      <div className="cmd" onMouseDown={(e) => e.stopPropagation()}>
        <div className="cmd-input">
          <Icon name="search" size={18} />
          <input ref={inputRef} value={q} onChange={(e) => setQ(e.target.value)} placeholder={t("Jump to a screen, find a student, or ask AI…")} />
          <kbd className="mono">esc</kbd>
        </div>
        <div className="cmd-body scroll-y">
          {aiActions.length ? (
            <div className="cmd-grp">
              <div className="cmd-glabel">{t("AI actions")}</div>
              {aiActions.map((a) => (
                <button key={a} className="cmd-item" onClick={() => onAssistant(a)}>
                  <span className="cmd-ic ai"><Icon name="spark" size={15} /></span>
                  <span>{a}</span><Icon name="arrowR" size={14} className="cmd-go" />
                </button>
              ))}
            </div>
          ) : null}
          {screenHits.length ? (
            <div className="cmd-grp">
              <div className="cmd-glabel">{t("Screens")}</div>
              {screenHits.map((k) => (
                <button key={k} className="cmd-item" onClick={() => { navigate(k); onClose(); }}>
                  <span className="cmd-ic"><Icon name={NAV.flatMap((g) => g.items).find((i) => i.key === k)?.icon || "dashboard"} size={15} /></span>
                  <span>{t(SCREEN_TITLES[k])}</span><Icon name="arrowR" size={14} className="cmd-go" />
                </button>
              ))}
            </div>
          ) : null}
          {studentHits.length ? (
            <div className="cmd-grp">
              <div className="cmd-glabel">{t("Students")}</div>
              {studentHits.map((s) => (
                <button key={s.id} className="cmd-item" onClick={() => { navigate("student/" + s.id); onClose(); }}>
                  <Avatar name={s.name} color={s.avatar} size={22} />
                  <span>{s.name}</span>
                  <span className="muted mono" style={{ fontSize: 11 }}>{s.cls} · {s.prog}</span>
                  <span style={{ marginLeft: "auto" }}><RiskScore score={s.risk} size="sm" /></span>
                </button>
              ))}
            </div>
          ) : null}
          {!screenHits.length && !studentHits.length && !aiActions.length ? (
            <div className="cmd-empty muted">{t("No matches for")} “{q}”.</div>
          ) : null}
        </div>
        <div className="cmd-foot">
          <span><kbd className="mono">↵</kbd> {t("open")}</span>
          <span><kbd className="mono">⌘K</kbd> {t("toggle")}</span>
          <span className="ml-auto">{t("Al-Beruni Academic Portal")}</span>
        </div>
      </div>
    </div>
  );
}

/* ---------- Assistant drawer ---------- */
function AssistantPanel({ state, role, onClose }) {
  // store English source strings; translate at render time so they follow the language toggle
  const [msgs, setMsgs] = React.useState([{
    from: "ai", text: "Hello. I'm your role-aware assistant. I can explain a student's risk, draft messages and report comments, and answer from the school's policies — with citations. I only ever draft; you approve.",
    sources: [], actions: ["Summarise this student's risk", "Find the late-submission policy", "Draft a report comment"],
  }]);
  const [input, setInput] = React.useState("");
  const [typing, setTyping] = React.useState(false);
  const bodyRef = React.useRef(null);
  const seededRef = React.useRef(null);

  const send = React.useCallback((text) => {
    if (!text.trim()) return;
    setMsgs((m) => [...m, { from: "me", text }]);
    setInput("");
    setTyping(true);
    setTimeout(() => {
      const r = window.AI.respond(text);
      setTyping(false);
      setMsgs((m) => [...m, { from: "ai", text: r.text, sources: r.sources, actions: r.actions, draft: r.draftHint || /draft/i.test(text) }]);
    }, 650);
  }, []);

  React.useEffect(() => {
    if (state.open && state.seed && seededRef.current !== state.seed) {
      seededRef.current = state.seed;
      send(state.seed);
    }
    if (!state.open) seededRef.current = null;
  }, [state.open, state.seed, send]);

  React.useEffect(() => { bodyRef.current?.scrollTo(0, bodyRef.current.scrollHeight); }, [msgs, typing]);

  return (
    <div className={"asst" + (state.open ? " open" : "")}>
      <div className="asst-scrim" onClick={onClose}></div>
      <div className="asst-panel">
        <div className="asst-h">
          <div className="row" style={{ gap: 10 }}>
            <span className="asst-mark"><Icon name="spark" size={16} /></span>
            <div>
              <div style={{ fontWeight: 600, fontSize: 14 }}>{t("AI Assistant")}</div>
              <div className="muted mono" style={{ fontSize: 10.5 }}>{role === "director" ? t("Director context · full access") : t("Teacher context · your students")}</div>
            </div>
          </div>
          <button className="icon-btn" onClick={onClose}><Icon name="close" size={18} /></button>
        </div>

        <div className="asst-body scroll-y" ref={bodyRef}>
          {msgs.map((m, i) => (
            <div key={i} className={"asst-msg " + m.from}>
              {m.from === "ai" ? <span className="asst-msg-mark"><Icon name="spark" size={12} /></span> : null}
              <div className="asst-bubble">
                <div style={{ whiteSpace: "pre-wrap" }}>{t(m.text)}</div>
                {m.sources && m.sources.length ? (
                  <div className="asst-src">
                    <div className="eyebrow" style={{ marginBottom: 6 }}>{t("Sources")}</div>
                    {m.sources.map((s, j) => (
                      <div key={j} className="asst-src-item">
                        <Icon name="doc" size={13} />
                        <span><b>{s.doc}</b> · {s.ref}</span>
                        <span className="mono faint" style={{ marginLeft: "auto", fontSize: 10 }}>{s.v}</span>
                      </div>
                    ))}
                  </div>
                ) : null}
                {m.draft ? (
                  <div className="asst-draft">
                    <span className="badge accent"><Icon name="note" size={11} />{t("Draft — needs your approval")}</span>
                    <div className="row" style={{ gap: 6, marginLeft: "auto" }}>
                      <button className="btn btn-sm" onClick={() => window.toast(t("Draft sent back for editing"))}>{t("Edit")}</button>
                      <button className="btn btn-sm btn-primary" onClick={() => window.toast(t("Draft approved"))}>{t("Approve")}</button>
                    </div>
                  </div>
                ) : null}
                {m.actions && m.actions.length ? (
                  <div className="asst-chips">
                    {m.actions.map((a) => <button key={a} className="asst-chip" onClick={() => send(a)}>{t(a)}</button>)}
                  </div>
                ) : null}
              </div>
            </div>
          ))}
          {typing ? <div className="asst-msg ai"><span className="asst-msg-mark"><Icon name="spark" size={12} /></span><div className="asst-bubble"><span className="asst-typing"><i></i><i></i><i></i></span></div></div> : null}
        </div>

        <div className="asst-input">
          <input value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") send(input); }}
            placeholder={t("Ask, or request a draft…")} />
          <button className="icon-btn" onClick={() => send(input)} disabled={!input.trim()}><Icon name="send" size={17} /></button>
        </div>
        <div className="asst-foot muted">{t("Assistant drafts only — a human always reviews and approves. Answers cite school documents.")}</div>
      </div>
    </div>
  );
}

Object.assign(window, { CommandPalette, AssistantPanel });
