from __future__ import annotations

from pathlib import Path

from typer.testing import CliRunner

from eightmem.cli.main import app


runner = CliRunner()


def test_e2e_founder_journey_json(fake_home):
    project_root = Path(__file__).resolve().parents[1]
    chat = project_root / "examples" / "use_cases" / "founder_journey.json"
    out_file = project_root / "examples" / "sample_output" / "founder_context.md"

    init_res = runner.invoke(app, ["init"])
    assert init_res.exit_code == 0

    analyze_res = runner.invoke(app, ["analyze", str(chat)])
    assert analyze_res.exit_code == 0

    mirror_res = runner.invoke(app, ["mirror"])
    assert mirror_res.exit_code == 0
    assert "AI Believes About You" in mirror_res.stdout
    assert "Decisions:" in mirror_res.stdout
    assert "Possible Contradictions:" in mirror_res.stdout

    export_res = runner.invoke(app, ["export-context", "-o", str(out_file)])
    assert export_res.exit_code == 0
    assert out_file.exists()

    mem = fake_home / "memory"
    identity = (mem / "IDENTITY.md").read_text(encoding="utf-8").lower()
    prefs = (mem / "PREFERENCES.md").read_text(encoding="utf-8").lower()
    decisions = (mem / "DECISIONS.md").read_text(encoding="utf-8").lower()
    evolution = (mem / "EVOLUTION.md").read_text(encoding="utf-8").lower()
    corrections = (mem / "CORRECTIONS.md").read_text(encoding="utf-8").lower()

    assert "founder working on 8mem" in identity
    assert "ashish and i am a founder working on 8mem" not in identity
    assert "keep answers concise and practical." in prefs
    assert "avoid buzzwords." in prefs
    assert "avoid emojis." in prefs
    assert "launch a local-first cli first" in decisions
    assert "changed focus recently" in evolution
    assert "use common language instead." in corrections


def test_e2e_consulting_transcript_text(fake_home):
    project_root = Path(__file__).resolve().parents[1]
    chat = project_root / "examples" / "use_cases" / "consulting_transcript.txt"

    init_res = runner.invoke(app, ["init"])
    assert init_res.exit_code == 0

    analyze_res = runner.invoke(app, ["analyze", str(chat)])
    assert analyze_res.exit_code == 0

    mirror_res = runner.invoke(app, ["mirror"])
    assert mirror_res.exit_code == 0
    assert "Identity:" in mirror_res.stdout
    assert "Preferences:" in mirror_res.stdout
    assert "Decisions:" in mirror_res.stdout

    mem = fake_home / "memory"
    identity = (mem / "IDENTITY.md").read_text(encoding="utf-8").lower()
    prefs = (mem / "PREFERENCES.md").read_text(encoding="utf-8").lower()
    decisions = (mem / "DECISIONS.md").read_text(encoding="utf-8").lower()
    evolution = (mem / "EVOLUTION.md").read_text(encoding="utf-8").lower()

    assert "engineering consultant for fintech teams" in identity
    assert "keep answers short and structured." in prefs
    assert "avoid emojis." in prefs
    assert "prefers numbered steps." in prefs
    assert "ship an audit checklist first" in decisions
    assert "recently we moved to weekly deliverables" in evolution
    beliefs = (mem / "BELIEFS.md").read_text(encoding="utf-8").lower()
    assert "(empty)" in beliefs
