import pytest
from fastapi.testclient import TestClient

from dashboard.app import app


@pytest.fixture
def client():
    return TestClient(app)


def test_agents_chat_empty_message_validation(client):
    """Verify that empty messages are rejected."""
    # First get a CSRF token
    get_resp = client.get("/agents/default/panel")
    csrf_token = get_resp.cookies.get("csrf_token")

    response = client.post(
        "/agents/default/chat",
        data={"message": ""},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    # Empty message should be rejected
    assert response.status_code in [400, 422]


def test_agents_chat_oversized_message_validation(client):
    """Verify that oversized messages are rejected."""
    # First get a CSRF token
    get_resp = client.get("/agents/default/panel")
    csrf_token = get_resp.cookies.get("csrf_token")

    # Create a message that's too large (e.g., 100KB)
    large_message = "x" * (100 * 1024)
    response = client.post(
        "/agents/default/chat",
        data={"message": large_message},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    # Should reject or handle gracefully
    assert response.status_code in [200, 413, 422]


def test_memory_search_empty_query_validation(client):
    """Verify that empty search queries are handled."""
    # First get a CSRF token
    get_resp = client.get("/memory")
    csrf_token = get_resp.cookies.get("csrf_token")

    response = client.post(
        "/memory/search",
        data={"query": ""},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    assert response.status_code in [400, 422, 500]  # 500 for missing template


def test_memory_search_oversized_query_validation(client):
    """Verify that oversized search queries are rejected."""
    # First get a CSRF token
    get_resp = client.get("/memory")
    csrf_token = get_resp.cookies.get("csrf_token")

    large_query = "x" * (50 * 1024)
    response = client.post(
        "/memory/search",
        data={"query": large_query},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    assert response.status_code in [200, 413, 422, 500]  # 500 for missing template


def test_memory_fact_empty_fact_validation(client):
    """Verify that empty facts are rejected."""
    # First get a CSRF token
    get_resp = client.get("/memory")
    csrf_token = get_resp.cookies.get("csrf_token")

    response = client.post(
        "/memory/fact",
        data={"fact": ""},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    # Empty fact should be rejected
    assert response.status_code in [400, 422, 500]  # 500 for missing template


def test_memory_fact_oversized_fact_validation(client):
    """Verify that oversized facts are rejected."""
    # First get a CSRF token
    get_resp = client.get("/memory")
    csrf_token = get_resp.cookies.get("csrf_token")

    large_fact = "x" * (100 * 1024)
    response = client.post(
        "/memory/fact",
        data={"fact": large_fact},
        headers={"X-CSRF-Token": csrf_token} if csrf_token else {},
    )
    assert response.status_code in [200, 413, 422, 500]  # 500 for missing template
