#!/usr/bin/env python3
"""Verify unified Milvus schema with both Excel and Word documents."""

from pymilvus import connections, Collection
from src.core.config import settings
import json

# Connect to Milvus
connections.connect(host=settings.milvus_host, port=settings.milvus_port)
collection = Collection(settings.milvus_collection)
collection.load()

# Query total count
print("=" * 70)
print("Milvus Unified Schema Verification")
print("=" * 70)

total_count = collection.num_entities
print(f"\n📊 Total vectors: {total_count}")

# Query by document type
excel_expr = 'document_type == "excel"'
word_expr = 'document_type == "word"'

excel_results = collection.query(expr=excel_expr, output_fields=["document_id"], limit=10000)
word_results = collection.query(expr=word_expr, output_fields=["document_id"], limit=10000)

print(f"\n📊 Vectors by type:")
print(f"   Excel: {len(excel_results)}")
print(f"   Word: {len(word_results)}")

# Sample Excel record
print(f"\n📄 Sample Excel record:")
excel_sample = collection.query(
    expr=excel_expr,
    output_fields=["filename", "document_type", "text_content", "metadata"],
    limit=1
)
if excel_sample:
    rec = excel_sample[0]
    print(f"   Filename: {rec['filename']}")
    print(f"   Type: {rec['document_type']}")
    print(f"   Text (first 100 chars): {rec['text_content'][:100]}...")
    print(f"   Metadata: {json.dumps(rec['metadata'], indent=2, ensure_ascii=False)}")

# Sample Word record
print(f"\n📄 Sample Word record:")
word_sample = collection.query(
    expr=word_expr,
    output_fields=["filename", "document_type", "text_content", "metadata"],
    limit=1
)
if word_sample:
    rec = word_sample[0]
    print(f"   Filename: {rec['filename']}")
    print(f"   Type: {rec['document_type']}")
    print(f"   Text (first 100 chars): {rec['text_content'][:100]}...")
    print(f"   Metadata: {json.dumps(rec['metadata'], indent=2, ensure_ascii=False)}")

print("\n" + "=" * 70)
print("✅ Unified schema is working correctly!")
print("=" * 70)

connections.disconnect("default")
