"""Tests for Memory Health Score feature."""

import pytest

from locidb.core.db import LociDB
from locidb.core.health import HealthReport


@pytest.fixture
def db(tmp_path):
    loci = LociDB(path=tmp_path / "test.loci")
    yield loci
    loci.close()


class TestHealthScore:
    def test_empty_db_health(self, db):
        report = db.health()
        assert isinstance(report, HealthReport)
        assert report.overall_score < 20
        assert report.dimensions["coverage"] == 0
        assert any("No memories" in g for g in report.gaps)

    def test_health_improves_with_data(self, db):
        for i in range(10):
            db.memorize(f"Memory about topic {i}", entities=[f"topic{i}"])
        report = db.health()
        assert report.dimensions["coverage"] >= 50

    def test_health_includes_all_dimensions(self, db):
        db.memorize("Test", entities=["test"])
        report = db.health()
        expected = {"coverage", "freshness", "diversity", "connectivity", "persona"}
        assert set(report.dimensions.keys()) == expected

    def test_health_persona_dimension(self, db):
        report_before = db.health()
        assert report_before.dimensions["persona"] == 0

        db.set_persona("name", "Test User")
        db.set_persona("role", "Developer")
        db.set_persona("expertise", "Python")
        db.set_persona("preferences", "Dark mode")
        report_after = db.health()
        assert report_after.dimensions["persona"] == 100

    def test_health_connectivity(self, db):
        n1 = db.memorize("Project Alpha uses Python", entities=["alpha", "python"])
        n2 = db.memorize("Project Alpha deployed on AWS", entities=["alpha", "aws"])
        report = db.health()
        # Should have some connectivity since nodes share entity "alpha"
        assert report.dimensions["connectivity"] > 0

    def test_health_score_range(self, db):
        for i in range(5):
            db.memorize(f"Memory {i}", entities=[f"entity{i}"])
        report = db.health()
        assert 0 <= report.overall_score <= 100

    def test_health_stats(self, db):
        db.memorize("Test memory", entities=["test"])
        report = db.health()
        assert "node_count" in report.stats
        assert report.stats["node_count"] >= 1

    def test_health_scope_filter(self, db):
        db.memorize("Alpha data", entities=["alpha"], scope="alpha")
        db.memorize("Beta data", entities=["beta"], scope="beta")
        report = db.health(scope="alpha")
        assert report.stats["node_count"] == 1


class TestOnboardingQuestions:
    def test_all_questions_when_empty(self, db):
        questions = db.get_onboarding_questions()
        assert len(questions) >= 6  # At least one per persona field + general

    def test_fewer_questions_with_persona(self, db):
        db.set_persona("name", "Alice")
        db.set_persona("role", "Dev")
        questions = db.get_onboarding_questions()
        assert not any("name" in q.lower() and "your" in q.lower() and "address" in q.lower() for q in questions)

    def test_no_memory_question_with_data(self, db):
        for i in range(5):
            db.memorize(f"Memory {i}", entities=["test"])
        questions = db.get_onboarding_questions()
        assert not any("key decisions" in q.lower() for q in questions)


class TestHealthGapsAndSuggestions:
    def test_gaps_populated(self, db):
        report = db.health()
        assert isinstance(report.gaps, list)
        assert len(report.gaps) > 0

    def test_suggestions_populated(self, db):
        report = db.health()
        assert isinstance(report.suggestions, list)
        assert len(report.suggestions) > 0
