import os
from qdrant_client import QdrantClient
from qdrant_client.models import PointStruct

# ==========================================
# 1. SETUP YOUR CONNECTIONS
# ==========================================
# Connect to your old Qdrant Cloud (Source)
cloud_client = QdrantClient(
    url="https://369d1a18-9eaa-4bf9-be05-115c9f188387.us-west-1-0.aws.cloud.qdrant.io:6333",
    api_key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIiwic3ViamVjdCI6ImFwaS1rZXk6MDY3MmFiZDktOTRhZi00NWMyLWJhZDctNWZlNmI2MTRiZDBkIn0.z_En6ORE7oHyGRxBAAjkho8-b-cqTXbWOPUassDfbYw"
)

# Connect to your new VPS Server (Destination)
vps_client = QdrantClient(url="http://66.116.207.225:6333")

# We will loop through both of your collections!
COLLECTIONS_TO_MIGRATE = ["doormile_clients", "doormile_auth"]

for collection_name in COLLECTIONS_TO_MIGRATE:
    print(f"\n==========================================")
    print(f"🚀 Starting migration for: {collection_name}")
    print(f"==========================================")

    # ==========================================
    # 2. RECREATE COLLECTION ON VPS
    # ==========================================
    try:
        collection_info = cloud_client.get_collection(collection_name=collection_name)
    except Exception as e:
        print(f"Could not find collection '{collection_name}' on cloud. Skipping...")
        continue

    print(f"Recreating '{collection_name}' on your VPS with identical settings...")
    vps_client.recreate_collection(
        collection_name=collection_name,
        vectors_config=collection_info.config.params.vectors,
    )

    # ==========================================
    # 3. TRANSFER THE DATA IN BATCHES
    # ==========================================
    batch_size = 100
    offset = None
    total_migrated = 0

    print("Starting data transfer...")

    while True:
        # Fetch a batch of points from the cloud
        records, offset = cloud_client.scroll(
            collection_name=collection_name,
            limit=batch_size,
            offset=offset,
            with_payload=True,
            with_vectors=True
        )
        
        if not records:
            break
            
        # Convert records to PointStruct format
        points = [
            PointStruct(
                id=record.id,
                vector=record.vector,
                payload=record.payload
            )
            for record in records
        ]
        
        # Upload the batch to your VPS
        vps_client.upsert(
            collection_name=collection_name,
            points=points
        )
        
        total_migrated += len(points)
        print(f" -> Copied {total_migrated} vectors...")
        
        if offset is None:
            break

print("\n✅ Migration Complete! Both collections are safely on your VPS.")
