"""Phase-24 verification: AiFastDb Python bindings — full CRUD test."""

import tempfile
import shutil

from aifastdb import AiFastDb

def test_all():
    tmpdir = tempfile.mkdtemp(prefix="aifastdb_test_")
    print(f"Test dir: {tmpdir}")

    try:
        with AiFastDb(tmpdir, dimension=64) as db:
            print(f"DB: {db}")
            print(f"Path: {db.path}")
            print(f"Dimension: {db.dimension}")

            # ---- T24.2: Collection management ----
            print(f"Collections: {db.list_collections()}")
            col = db.collection("articles", dimension=64, description="Test articles")
            print(f"Created: {col}")
            has = db.has_collection("articles")
            print(f"has_collection(articles): {has}")
            print(f"list_collections: {db.list_collections()}")

            # ---- T24.3: Basic CRUD ----
            db.remember("doc1", "Hello world", tags=["greeting", "test"], metadata={"lang": "en"})
            db.remember("doc2", "Python is great", content_type="code", importance=0.8)
            db.remember("doc3", "Rust is fast", tags=["rust"], parent_id="doc1")
            print(f"Document count: {db.document_count()}")

            r1 = db.get("doc1")
            print(f"get(doc1): id={r1['id']}, content={r1['content']}, tags={r1.get('tags')}")
            assert r1["id"] == "doc1"
            assert r1["content"] == "Hello world"
            assert "greeting" in r1["tags"]

            r_none = db.get("nonexistent")
            print(f"get(nonexistent): {r_none}")
            assert r_none is None

            db.touch("doc1")
            r1_touched = db.get("doc1")
            print(f"touch(doc1): access_count={r1_touched['access_count']}")
            assert r1_touched["access_count"] >= 1

            deleted = db.forget("doc3")
            print(f"forget(doc3): {deleted}")
            assert deleted is True
            print(f"Document count after forget: {db.document_count()}")

            not_deleted = db.forget("nonexistent")
            assert not_deleted is False

            # ---- T24.4: Batch operations ----
            count = db.remember_many([
                {"id": "batch1", "content": "Batch item 1", "tags": ["batch"]},
                {"id": "batch2", "content": "Batch item 2", "tags": ["batch"]},
                {"id": "batch3", "content": "Batch item 3", "metadata": {"idx": 3}},
            ])
            print(f"remember_many: {count} items")
            assert count == 3

            many = db.get_many(["doc1", "batch1", "batch2", "nonexistent"])
            print(f"get_many: {len(many)} found, keys={list(many.keys())}")
            assert len(many) == 3
            assert "doc1" in many
            assert "nonexistent" not in many

            # ---- T24.5: recall_all ----
            all_docs = db.recall_all()
            print(f"recall_all: {len(all_docs)} documents")
            assert len(all_docs) >= 5  # doc1, doc2, batch1, batch2, batch3

            # ---- T24.8: Collection class ----
            col.remember("art1", "Article content", tags=["article"])
            print(f"Collection doc count: {col.document_count()}")
            art = col.get("art1")
            print(f"Collection get: id={art['id']}, content={art['content']}")
            assert art["id"] == "art1"
            col_all = col.recall_all()
            print(f"Collection recall_all: {len(col_all)}")
            assert len(col_all) >= 1

            # ---- T24.2: delete_collection ----
            deleted_col = db.delete_collection("articles")
            print(f"delete_collection(articles): {deleted_col}")
            has_after = db.has_collection("articles")
            print(f"has_collection after delete: {has_after}")

            # ---- __len__ ----
            print(f"len(db) = {len(db)}")
            assert len(db) >= 5

            db.flush()

        print()
        print("=" * 50)
        print("All phase-24 tests PASSED!")
        print("=" * 50)

    finally:
        shutil.rmtree(tmpdir, ignore_errors=True)


if __name__ == "__main__":
    test_all()
