"""Scroll with a filter, paginate, count, and delete all chunks of one url (the re-crawl path)."""
from qdrant_client import QdrantClient, models
from common import COLLECTION, read_chunks, section_of
import uuid

client = QdrantClient(url="http://localhost:6333")
URL = "https://webcrawlerapi.com/docs/api/crawl"
by_url = models.Filter(must=[models.FieldCondition(key="url", match=models.MatchValue(value=URL))])

print("collection points_count:", client.get_collection(COLLECTION).points_count)
print("payload_schema:", {k: v.data_type.value for k, v in client.get_collection(COLLECTION).payload_schema.items()})

# Scroll the 'errors' section, 5 per page, follow next_page_offset
flt = models.Filter(must=[models.FieldCondition(key="section", match=models.MatchValue(value="errors"))])
offset, page, seen = None, 0, 0
while True:
    pts, offset = client.scroll(COLLECTION, scroll_filter=flt, limit=5, offset=offset, with_payload=["heading", "url"], with_vectors=False)
    page += 1
    seen += len(pts)
    print(f"page {page}: {len(pts)} points, first heading: {pts[0].payload['heading']!r}, next_page_offset: {offset}")
    if offset is None:
        break
print("errors chunks seen via scroll:", seen)

# Delete by filter, then put the chunks back
before = client.count(COLLECTION, exact=True).count
n_url = client.count(COLLECTION, count_filter=by_url, exact=True).count
print(f"before: total={before}, chunks for {URL}: {n_url}")
client.delete(COLLECTION, points_selector=models.FilterSelector(filter=by_url), wait=True)
print("after delete: total =", client.count(COLLECTION, exact=True).count)

NS = uuid.UUID("6f1c5a3e-0000-4000-8000-000000000000")
rows, _, _ = read_chunks()
back = [
    models.PointStruct(id=str(uuid.uuid5(NS, r["id"])), vector=r["embedding"],
                       payload={"chunk_id": r["id"], "url": r["url"], "heading": r["heading"], "section": section_of(r["url"]), "text": r["text"]})
    for r in rows if r["url"] == URL
]
client.upsert(COLLECTION, points=back, wait=True)
print("after re-upsert: total =", client.count(COLLECTION, exact=True).count)
