"""Tests for BigMind people/contacts directory (v3.0 — schema v7)."""
import pytest
from bigmind import memory_store


@pytest.fixture
def user(temp_db):
    return memory_store.get_or_create_user("testuser", "Test User")


class TestRememberPerson:
    def test_insert_minimal(self, temp_db, user):
        pid = memory_store.upsert_person(user["id"], "elias")
        assert pid > 0

    def test_insert_full(self, temp_db, user):
        pid = memory_store.upsert_person(
            user["id"], "elias",
            display_name="Elias Müller",
            role="Engineer",
            team="PI",
            notes="Shared BigMind with him",
            bigmind_user="elias",
            bigmind_url="http://localhost:7701",
        )
        people = memory_store.list_people(user["id"])
        assert len(people) == 1
        p = people[0]
        assert p["display_name"] == "Elias Müller"
        assert p["bigmind_user"] == "elias"
        assert p["bigmind_url"] == "http://localhost:7701"

    def test_upsert_updates_existing(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias", role="Intern")
        memory_store.upsert_person(user["id"], "elias", role="Engineer")
        people = memory_store.list_people(user["id"])
        assert len(people) == 1
        assert people[0]["role"] == "Engineer"

    def test_upsert_preserves_unset_fields(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias", notes="First note")
        memory_store.upsert_person(user["id"], "elias", role="Engineer")
        people = memory_store.list_people(user["id"])
        assert people[0]["notes"] == "First note"
        assert people[0]["role"] == "Engineer"

    def test_different_users_isolated(self, temp_db):
        u1 = memory_store.get_or_create_user("alice")
        u2 = memory_store.get_or_create_user("bob")
        memory_store.upsert_person(u1["id"], "elias")
        assert memory_store.list_people(u2["id"]) == []


class TestRecallPerson:
    def test_search_by_name(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias", display_name="Elias Müller")
        memory_store.upsert_person(user["id"], "klaus", display_name="Klaus Schmidt")
        results = memory_store.recall_person(user["id"], "elias")
        assert len(results) == 1
        assert results[0]["username"] == "elias"

    def test_search_by_role(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias", role="Frontend Engineer")
        memory_store.upsert_person(user["id"], "oliver", role="Backend Engineer")
        results = memory_store.recall_person(user["id"], "Frontend")
        assert len(results) == 1
        assert results[0]["username"] == "elias"

    def test_search_by_notes(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias", notes="token efficiency idea")
        results = memory_store.recall_person(user["id"], "token")
        assert len(results) == 1

    def test_no_results(self, temp_db, user):
        results = memory_store.recall_person(user["id"], "nobody")
        assert results == []

    def test_search_isolated_by_user(self, temp_db):
        u1 = memory_store.get_or_create_user("alice")
        u2 = memory_store.get_or_create_user("bob")
        memory_store.upsert_person(u1["id"], "elias", role="Engineer")
        results = memory_store.recall_person(u2["id"], "Engineer")
        assert results == []


class TestListPeople:
    def test_empty(self, temp_db, user):
        assert memory_store.list_people(user["id"]) == []

    def test_returns_all(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias")
        memory_store.upsert_person(user["id"], "klaus")
        memory_store.upsert_person(user["id"], "oliver")
        assert len(memory_store.list_people(user["id"])) == 3


class TestLinkAI:
    def test_link_existing_person(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias")
        result = memory_store.link_ai(user["id"], "elias", "elias_ai", "http://localhost:7701")
        assert result is True
        people = memory_store.list_people(user["id"])
        assert people[0]["bigmind_user"] == "elias_ai"
        assert people[0]["bigmind_url"] == "http://localhost:7701"

    def test_link_nonexistent_person(self, temp_db, user):
        result = memory_store.link_ai(user["id"], "ghost", "ghost_ai")
        assert result is False

    def test_link_without_url(self, temp_db, user):
        memory_store.upsert_person(user["id"], "elias")
        result = memory_store.link_ai(user["id"], "elias", "elias_ai")
        assert result is True
        people = memory_store.list_people(user["id"])
        assert people[0]["bigmind_user"] == "elias_ai"
        assert people[0]["bigmind_url"] is None
