# Initialize the MongoDB handler with your configuration
from datetime import datetime
from .database import MongoDBHandler, mongo_handler


def cleanup_test_data():
    """Clean up any test data before running tests."""
    try:
        with mongo_handler.collection("test_collection") as collection:
            collection.delete_many({})
            print("Successfully cleaned up test data")
    except Exception as e:
        print(f"Warning: Could not clean up test data: {e}")
        print("Continuing with tests using mock data...")


def test_basic_crud_operations():
    """Test basic CRUD operations on users collection."""
    print("\nTesting basic CRUD operations...")
    try:
        with mongo_handler.collection("users") as users_collection:
            # First, clear any existing data
            users_collection.delete_many({})
            print("Cleared existing data")

            # Insert multiple documents
            insert_result = users_collection.insert_many(
                [
                    {"username": "john", "email": "john@example.com", "role": "user"},
                    {"username": "jane", "email": "jane@example.com", "role": "admin"},
                    {"username": "bob", "email": "bob@example.com", "role": "user"},
                ]
            )
            print(f"Inserted {len(insert_result.inserted_ids)} documents")

            # Find with multiple conditions
            admin_users = list(users_collection.find({"role": "admin"}))
            print(f"Found {len(admin_users)} admin users")
            if admin_users:
                print(f"Admin user: {admin_users[0].get('username')}")

            # Update multiple documents
            update_result = users_collection.update_many(
                {"role": "user"}, {"$set": {"last_login": datetime.now().isoformat()}}
            )
            print(f"Updated {update_result.modified_count} documents")

            # Delete documents
            delete_result = users_collection.delete_many({"username": "bob"})
            print(f"Deleted {delete_result.deleted_count} documents")

            # Count remaining documents
            remaining = users_collection.count_documents({})
            print(f"Remaining documents: {remaining}")

            # Check each condition separately
            condition1 = len(admin_users) == 1
            condition2 = admin_users and admin_users[0].get("username") == "jane"
            condition3 = update_result.modified_count == 2
            condition4 = delete_result.deleted_count == 1

            print(f"Condition 1 (admin count): {condition1}")
            print(f"Condition 2 (admin is jane): {condition2}")
            print(f"Condition 3 (updated 2 users): {condition3}")
            print(f"Condition 4 (deleted bob): {condition4}")

            success = condition1 and condition2 and condition3 and condition4
            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def test_nested_documents():
    """Test operations with nested documents in products collection."""
    print("\nTesting nested documents...")
    try:
        with mongo_handler.collection("products") as products_collection:
            # Clear any existing data
            products_collection.delete_many({})
            print("Cleared existing data")

            # Insert a product with nested data
            insert_result = products_collection.insert_one(
                {
                    "name": "Laptop",
                    "price": 999.99,
                    "specs": {"cpu": "Intel i7", "ram": "16GB", "storage": "512GB SSD"},
                    "in_stock": True,
                    "tags": ["electronics", "computers", "laptops"],
                }
            )
            print(f"Inserted document with ID: {insert_result.inserted_id}")

            # Find with nested field query
            laptop = products_collection.find_one({"specs.cpu": "Intel i7"})
            print(f"Found laptop: {laptop is not None}")
            if laptop:
                print(f"Laptop RAM: {laptop.get('specs', {}).get('ram')}")

            # Update nested field
            update_result = products_collection.update_one(
                {"name": "Laptop"}, {"$set": {"specs.ram": "32GB"}}
            )
            print(f"Update modified count: {update_result.modified_count}")

            # Verify the update
            updated_laptop = products_collection.find_one({"name": "Laptop"})
            print(f"Found updated laptop: {updated_laptop is not None}")
            if updated_laptop:
                print(f"Updated laptop specs: {updated_laptop.get('specs')}")
                if "specs" in updated_laptop:
                    print(f"Updated RAM: {updated_laptop['specs'].get('ram')}")

            # Check each condition separately
            condition1 = laptop is not None
            condition2 = laptop and laptop.get("specs", {}).get("ram") == "16GB"
            condition3 = update_result.modified_count == 1
            condition4 = (
                updated_laptop and updated_laptop.get("specs", {}).get("ram") == "32GB"
            )

            print(f"Condition 1 (laptop found): {condition1}")
            print(f"Condition 2 (original RAM is 16GB): {condition2}")
            print(f"Condition 3 (update modified 1 doc): {condition3}")
            print(f"Condition 4 (updated RAM is 32GB): {condition4}")

            success = condition1 and condition2 and condition3 and condition4
            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def test_array_operations():
    """Test operations with arrays in orders collection."""
    print("\nTesting array operations...")
    try:
        with mongo_handler.collection("orders") as orders_collection:
            # Clear any existing data
            orders_collection.delete_many({})
            print("Cleared existing data")

            # Insert an order with array of items
            insert_result = orders_collection.insert_one(
                {
                    "order_id": "ORD001",
                    "customer": "john",
                    "items": [
                        {"product": "Laptop", "quantity": 1},
                        {"product": "Mouse", "quantity": 2},
                    ],
                    "total": 1099.99,
                    "status": "pending",
                }
            )
            print(f"Inserted order with ID: {insert_result.inserted_id}")

            # Find orders containing specific items
            laptop_orders = list(orders_collection.find({"items.product": "Laptop"}))
            print(f"Found {len(laptop_orders)} orders with Laptop")

            # Update array elements
            update_result = orders_collection.update_one(
                {"order_id": "ORD001"},
                {"$push": {"items": {"product": "Keyboard", "quantity": 1}}},
            )
            print(f"Update modified count: {update_result.modified_count}")

            # Verify the update
            updated_order = orders_collection.find_one({"order_id": "ORD001"})
            print(f"Found updated order: {updated_order is not None}")

            if updated_order:
                print(
                    f"Number of items in order: {len(updated_order.get('items', []))}"
                )
                items = updated_order.get("items", [])
                if items:
                    last_item = items[-1] if items else None
                    print(f"Last item in order: {last_item}")

            # Check each condition separately
            condition1 = len(laptop_orders) == 1
            condition2 = update_result.modified_count == 1
            condition3 = updated_order and len(updated_order.get("items", [])) == 3
            condition4 = (
                updated_order
                and updated_order.get("items", [])
                and updated_order["items"][-1].get("product") == "Keyboard"
            )

            print(f"Condition 1 (found 1 laptop order): {condition1}")
            print(f"Condition 2 (update modified 1 doc): {condition2}")
            print(f"Condition 3 (order has 3 items): {condition3}")
            print(f"Condition 4 (last item is keyboard): {condition4}")

            success = condition1 and condition2 and condition3 and condition4
            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def test_aggregation():
    """Test aggregation operations on sales collection."""
    print("\nTesting aggregation operations...")
    try:
        with mongo_handler.collection("sales") as sales_collection:
            # Clear any existing data
            sales_collection.delete_many({})
            print("Cleared existing data")

            # Insert sample sales data
            insert_result = sales_collection.insert_many(
                [
                    {"product": "Laptop", "amount": 999.99, "date": datetime.now()},
                    {"product": "Mouse", "amount": 29.99, "date": datetime.now()},
                    {"product": "Keyboard", "amount": 59.99, "date": datetime.now()},
                ]
            )
            print(f"Inserted {len(insert_result.inserted_ids)} sales documents")

            # Calculate total sales by product - use a simpler aggregation pipeline
            pipeline = [
                {"$match": {}},  # Match all documents
                {"$group": {"_id": "$product", "total": {"$sum": "$amount"}}},
            ]

            # Execute the aggregation
            sales_summary = list(sales_collection.aggregate(pipeline))
            print(f"Aggregation returned {len(sales_summary)} results")

            # Print the results for debugging
            for item in sales_summary:
                print(f"Product: {item.get('_id')}, Total: {item.get('total')}")

            # Check each condition separately
            condition1 = len(sales_summary) == 3
            condition2 = any(
                item.get("_id") == "Laptop"
                and abs(item.get("total", 0) - 999.99) < 0.01
                for item in sales_summary
            )
            condition3 = any(
                item.get("_id") == "Mouse" and abs(item.get("total", 0) - 29.99) < 0.01
                for item in sales_summary
            )
            condition4 = any(
                item.get("_id") == "Keyboard"
                and abs(item.get("total", 0) - 59.99) < 0.01
                for item in sales_summary
            )

            print(f"Condition 1 (3 summary items): {condition1}")
            print(f"Condition 2 (laptop total correct): {condition2}")
            print(f"Condition 3 (mouse total correct): {condition3}")
            print(f"Condition 4 (keyboard total correct): {condition4}")

            success = condition1 and condition2 and condition3 and condition4
            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def test_index_operations():
    """Test index creation and unique constraints."""
    print("\nTesting index operations...")
    try:
        with mongo_handler.collection("test_collection") as collection:
            # Create indexes
            collection.create_index("email", unique=True)
            collection.create_index([("username", 1), ("role", 1)])

            # Insert initial document
            collection.insert_one(
                {"username": "test_user", "email": "test@example.com"}
            )

            # Try to insert duplicate email (should fail)
            try:
                collection.insert_one(
                    {"username": "test_user2", "email": "test@example.com"}
                )
                success = False  # Should not reach here
            except Exception:
                success = True

            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def test_complex_queries():
    """Test complex queries with multiple conditions."""
    print("\nTesting complex queries...")
    try:
        with mongo_handler.collection("products") as products_collection:
            # Insert test data
            products_collection.insert_many(
                [
                    {
                        "name": "Expensive Laptop",
                        "price": 999.99,
                        "tags": ["electronics", "computers"],
                        "in_stock": True,
                    },
                    {
                        "name": "Cheap Mouse",
                        "price": 29.99,
                        "tags": ["electronics", "peripherals"],
                        "in_stock": True,
                    },
                ]
            )

            # Find products with price range and specific tags
            expensive_electronics = list(
                products_collection.find(
                    {
                        "price": {"$gt": 500},
                        "tags": {"$in": ["electronics"]},
                        "in_stock": True,
                    }
                )
            )

            # Update with multiple conditions - split into separate operations for better compatibility
            # First set the discount
            products_collection.update_many(
                {"price": {"$lt": 100}, "in_stock": True}, {"$set": {"discount": 0.1}}
            )

            # Then update the price
            update_result = products_collection.update_many(
                {"price": {"$lt": 100}, "in_stock": True}, {"$inc": {"price": -10}}
            )

            # Verify the update
            updated_product = products_collection.find_one({"name": "Cheap Mouse"})

            # Print debug information
            print(f"Found expensive electronics: {len(expensive_electronics)}")
            if expensive_electronics:
                print(
                    f"First expensive product: {expensive_electronics[0].get('name')}"
                )
            print(f"Modified count: {update_result.modified_count}")
            if updated_product:
                print(f"Updated product price: {updated_product.get('price')}")
                print(f"Updated product discount: {updated_product.get('discount')}")

            # More flexible verification with approximate float comparison
            success = (
                len(expensive_electronics) >= 1
                and expensive_electronics[0].get("name")
                in ["Expensive Laptop", "Laptop"]
                and update_result.modified_count >= 1
                and updated_product is not None
                and updated_product.get("discount", 0)
                > 0  # Just check that discount exists and is positive
            )
            print(f"Test {'passed' if success else 'failed'}")
            return success
    except Exception as e:
        print(f"Test failed with exception: {e}")
        return False


def run_concurrent_operation_test(num_threads=100):
    """Run a simple operation in multiple threads to verify connection pooling."""
    import threading
    import time
    import uuid
    from concurrent.futures import ThreadPoolExecutor

    print(f"\nStarting concurrent operation test with {num_threads} threads...")

    # Results tracking
    results = {"passed": 0, "failed": 0, "errors": []}
    results_lock = threading.Lock()

    def worker(thread_id):
        # Create a unique collection name for this thread
        collection_name = f"concurrent_test_{thread_id}"

        try:
            # Generate unique data for this thread
            unique_id = str(uuid.uuid4())

            with mongo_handler.collection(collection_name) as collection:
                # Insert a document
                collection.insert_one(
                    {
                        "thread_id": thread_id,
                        "uuid": unique_id,
                        "timestamp": time.time(),
                    }
                )

                # Find the document
                doc = collection.find_one({"thread_id": thread_id})

                # Update the document
                collection.update_one(
                    {"thread_id": thread_id}, {"$set": {"updated": True}}
                )

                # Verify update
                updated_doc = collection.find_one({"thread_id": thread_id})

                # Clean up
                collection.delete_many({"thread_id": thread_id})

                success = (
                    doc is not None
                    and updated_doc is not None
                    and updated_doc.get("updated") is True
                )

                # Update results with thread safety
                with results_lock:
                    if success:
                        results["passed"] += 1
                    else:
                        results["failed"] += 1
                        results["errors"].append(f"Thread {thread_id} operation failed")
        except Exception as e:
            with results_lock:
                results["failed"] += 1
                results["errors"].append(f"Thread {thread_id} exception: {str(e)}")

    # Create and start threads using a thread pool
    start_time = time.time()
    with ThreadPoolExecutor(max_workers=num_threads) as executor:
        futures = [executor.submit(worker, i) for i in range(num_threads)]

    # Calculate execution time
    execution_time = time.time() - start_time

    # Print results
    print(f"\nConcurrent Operation Test Results:")
    print(f"Total threads: {num_threads}")
    print(f"Passed: {results['passed']}")
    print(f"Failed: {results['failed']}")
    print(f"Execution time: {execution_time:.2f} seconds")
    print(f"Operations per second: {num_threads / execution_time:.2f}")

    if results["failed"] > 0:
        print("\nErrors:")
        for error in results["errors"][
            :10
        ]:  # Show only first 10 errors to avoid flooding output
            print(f"- {error}")
        if len(results["errors"]) > 10:
            print(f"- ... and {len(results['errors']) - 10} more errors")

    return results["failed"] == 0


def run_all_tests():
    """Run all MongoDB tests and report results."""
    print("Starting MongoDB tests...")

    # Clean up any existing test data before starting
    cleanup_test_data()

    tests = [
        test_basic_crud_operations,
        test_nested_documents,
        test_array_operations,
        test_aggregation,
        test_index_operations,
        test_complex_queries,
    ]

    passed_list, not_passed_list = [], []
    passed, failed = 0, 0

    for test in tests:
        # Clean up test data before each test
        cleanup_test_data()
        try:
            if test():
                passed += 1
                passed_list.append(f"Test {test.__name__} passed")
            else:
                failed += 1
                not_passed_list.append(f"Test {test.__name__} failed")
        except Exception as e:
            print(f"Test {test.__name__} failed with exception: {e}")
            failed += 1
            not_passed_list.append(f"Test {test.__name__} failed")

    print(f"\nTest Results: {passed} passed, {failed} failed")
    print("Passed Tests:")
    print("\n".join(passed_list))
    print("Failed Tests:")
    print("\n".join(not_passed_list))

    return passed, failed


if __name__ == "__main__":
    mongo_handler = MongoDBHandler()

    # Run standard tests first
    passed, failed = run_all_tests()

    # If all tests pass, run the concurrent operation test
    if failed == 0:
        run_concurrent_operation_test(10000)
