from __future__ import annotations

from pathlib import Path

import streamlit as st


@st.cache_data(show_spinner=False)
def load_help_content() -> tuple[str, bool]:
    app_path = Path(__file__).resolve().parents[1] / "app.py"
    help_html_paths = [
        app_path.with_name("web_core").joinpath("help.html"),
        app_path.with_name("help.html"),
    ]
    for help_html_path in help_html_paths:
        if help_html_path.exists():
            return help_html_path.read_text(encoding="utf-8"), True

    onboarding_path = app_path.with_name("ONBOARDING.md")
    if onboarding_path.exists():
        return onboarding_path.read_text(encoding="utf-8"), False
    return "Help content not found.", False


@st.dialog("Help & Quick Start", width="large")
def help_dialog() -> None:
    content, is_html = load_help_content()
    st.markdown(content, unsafe_allow_html=is_html)
