import gc
import pytest
import random
import time
import tempfile
import os
from fastapi.testclient import TestClient
from pixlvault.server import Server

BACKEND_URL = "http://localhost:9537"


@pytest.mark.parametrize(
    "params",
    [
        {},
        {"sort": "SCORE", "descending": True},
        {"sort": "DATE", "descending": False},
    ],
)
def test_order_stability(params):
    """
    For each set of parameters, repeatedly query the backend and check that the returned image IDs are always in the same order.
    """
    with tempfile.TemporaryDirectory() as temp_dir:
        image_root = os.path.join(temp_dir, "images")
        os.makedirs(image_root, exist_ok=True)
        server_config_path = os.path.join(temp_dir, "server-config.json")

        with Server(server_config_path) as server:
            server.vault.import_default_data(True)
            client = TestClient(server.api)

            resp = client.post(
                "/login", json={"username": "testuser", "password": "testpassword"}
            )
            assert resp.status_code == 200
            print("Login Response: ", resp.json())  # Should include "session_id"
            assert "session_id" in client.cookies, (
                "session_id cookie not set after login"
            )

            resp = client.get("/protected")
            print("Protected Response: ", resp.json())

            first_ids = []

            for i in range(0, 3):
                time.sleep(random.uniform(0.01, 0.05))
                resp = client.get("/pictures", params={**params})
                assert resp.status_code == 200, (
                    f"Backend returned {resp.status_code} for params {params}"
                )
                data = resp.json()
                ids = [img["id"] for img in data if "id" in img]
                if i == 0:
                    first_ids = ids
                else:
                    assert ids == first_ids, (
                        f"Order not stable for params {params}: {ids} != {first_ids}"
                    )
    gc.collect()
