"""Tests for shared Database query methods added in the DB refactor."""

from __future__ import annotations


# ── rated_count ───────────────────────────────────────────────────────


def test_rated_count_returns_int(seeded_db):
    result = seeded_db.rated_count()
    assert isinstance(result, int)
    assert result == 5  # seeded_db has 5 ratings


def test_rated_count_empty(tmp_db):
    result = tmp_db.rated_count()
    assert result == 0


# ── gap_count ─────────────────────────────────────────────────────────


def test_gap_count_empty(tmp_db):
    result = tmp_db.gap_count()
    assert isinstance(result, int)
    assert result == 0


def test_gap_count_after_insert(seeded_db):
    seeded_db.conn.execute(
        "INSERT INTO gaps (topic, description, category, severity) VALUES (?, ?, ?, ?)",
        ("Test Gap", "A test gap", "A2A protocols", "high"),
    )
    seeded_db.conn.commit()
    assert seeded_db.gap_count() == 1


# ── false_positive_names ──────────────────────────────────────────────


def test_false_positive_names_empty(seeded_db):
    result = seeded_db.false_positive_names()
    assert isinstance(result, set)
    assert len(result) == 0


def test_false_positive_names_after_flag(seeded_db):
    seeded_db.conn.execute(
        "UPDATE ratings SET false_positive = 1 WHERE draft_name = 'draft-alpha-agent-comm'"
    )
    seeded_db.conn.commit()
    result = seeded_db.false_positive_names()
    assert "draft-alpha-agent-comm" in result
    assert len(result) == 1


# ── category_counts ──────────────────────────────────────────────────


def test_category_counts_returns_dict(seeded_db):
    result = seeded_db.category_counts()
    assert isinstance(result, dict)
    assert len(result) > 0


def test_category_counts_values_are_ints(seeded_db):
    result = seeded_db.category_counts()
    for k, v in result.items():
        assert isinstance(k, str)
        assert isinstance(v, int)
        assert v > 0


# ── source_counts ────────────────────────────────────────────────────


def test_source_counts_returns_list(seeded_db):
    result = seeded_db.source_counts()
    assert isinstance(result, list)


def test_source_counts_tuples(seeded_db):
    result = seeded_db.source_counts()
    for item in result:
        assert isinstance(item, tuple)
        assert len(item) == 2


# ── draft_author_count_map ───────────────────────────────────────────


def test_draft_author_count_map_returns_dict(seeded_db):
    result = seeded_db.draft_author_count_map()
    assert isinstance(result, dict)
    # seeded_db has 4 drafts with authors
    assert len(result) >= 3


def test_draft_author_count_map_values(seeded_db):
    result = seeded_db.draft_author_count_map()
    # draft-alpha-agent-comm has 2 authors
    assert result.get("draft-alpha-agent-comm") == 2
    # draft-beta-ml-traffic has 1 author
    assert result.get("draft-beta-ml-traffic") == 1


def test_draft_author_count_map_empty(tmp_db):
    result = tmp_db.draft_author_count_map()
    assert result == {}


# ── search_gaps ──────────────────────────────────────────────────────


def test_search_gaps_empty_db(tmp_db):
    result = tmp_db.search_gaps("anything")
    assert isinstance(result, list)
    assert len(result) == 0


def test_search_gaps_finds_match(seeded_db):
    seeded_db.conn.execute(
        "INSERT INTO gaps (topic, description, category, severity) VALUES (?, ?, ?, ?)",
        ("Agent Discovery Gap", "No standard for agent discovery", "Agent discovery/reg", "high"),
    )
    seeded_db.conn.commit()
    result = seeded_db.search_gaps("discovery")
    assert len(result) == 1
    assert result[0]["topic"] == "Agent Discovery Gap"


def test_search_gaps_no_match(seeded_db):
    seeded_db.conn.execute(
        "INSERT INTO gaps (topic, description, category, severity) VALUES (?, ?, ?, ?)",
        ("Agent Discovery Gap", "No standard", "Agent discovery/reg", "high"),
    )
    seeded_db.conn.commit()
    result = seeded_db.search_gaps("zzz-nonexistent-zzz")
    assert len(result) == 0


# ── search_authors ───────────────────────────────────────────────────


def test_search_authors_by_name(seeded_db):
    result = seeded_db.search_authors("Alice")
    assert len(result) >= 1
    assert any(a["name"] == "Alice Researcher" for a in result)


def test_search_authors_by_affiliation(seeded_db):
    result = seeded_db.search_authors("ExampleCorp")
    assert len(result) >= 2  # Alice and Carol


def test_search_authors_no_match(seeded_db):
    result = seeded_db.search_authors("zzz-nonexistent-zzz")
    assert len(result) == 0


def test_search_authors_empty_db(tmp_db):
    result = tmp_db.search_authors("anyone")
    assert isinstance(result, list)
    assert len(result) == 0


# ── search_authors dict shape ────────────────────────────────────────


def test_search_authors_result_shape(seeded_db):
    result = seeded_db.search_authors("Alice")
    for item in result:
        assert "person_id" in item
        assert "name" in item
        assert "affiliation" in item
