"""
Research API Tests with Pytest

⚠️ IMPORTANT: THESE ARE REAL INTEGRATION TESTS ⚠️

These tests run against a REAL running LDR server and perform ACTUAL operations.
They use Puppeteer for authentication and pytest for test execution and validation.
"""

import json
import subprocess
import time
import pytest
import requests
import os as os_module
from pathlib import Path
import tempfile
import os

# Base URL for tests
BASE_URL = os.environ.get("LDR_TEST_BASE_URL", "http://127.0.0.1:5000")
TEST_USERNAME = f"testuser_{int(time.time())}"
TEST_PASSWORD = "testpass123"


class AuthHelper:
    """Helper class to handle Puppeteer authentication"""

    @staticmethod
    def get_auth_cookies():
        """Use Puppeteer to authenticate and get cookies"""
        # Create a temporary file for cookie storage
        cookie_file = tempfile.NamedTemporaryFile(
            mode="w", suffix=".json", delete=False
        )
        cookie_file.close()

        # Run the Node.js auth helper
        auth_script = Path(__file__).parent / "auth_helper.js"
        cmd = [
            "node",
            str(auth_script),
            BASE_URL,
            TEST_USERNAME,
            TEST_PASSWORD,
            cookie_file.name,
        ]

        try:
            result = subprocess.run(
                cmd, capture_output=True, text=True, timeout=120
            )
            if result.returncode != 0:
                raise Exception(f"Auth failed: {result.stderr}")

            # Read cookies from file
            with open(cookie_file.name, "r") as f:
                cookies = json.load(f)

            # Convert to requests format
            cookie_dict = {c["name"]: c["value"] for c in cookies}

            # Extract CSRF token
            csrf_token = None
            for cookie in cookies:
                if cookie["name"] == "csrf_token":
                    csrf_token = cookie["value"]
                    break

            return cookie_dict, csrf_token

        finally:
            # Clean up temp file
            if Path(cookie_file.name).exists():
                os.unlink(cookie_file.name)


@pytest.fixture(scope="session")
def auth_session():
    """Session-wide fixture for authenticated requests"""
    # Skip in CI - requires running server
    if (
        os_module.environ.get("CI") == "true"
        or os_module.environ.get("GITHUB_ACTIONS") == "true"
    ):
        pytest.skip("Skipping API tests in CI - requires running server")
    cookies, csrf_token = AuthHelper.get_auth_cookies()

    session = requests.Session()
    session.cookies.update(cookies)
    session.headers.update(
        {"X-CSRFToken": csrf_token, "Accept": "application/json"}
    )

    yield session, csrf_token

    session.close()


@pytest.mark.skipif(
    os_module.environ.get("CI") == "true"
    or os_module.environ.get("GITHUB_ACTIONS") == "true",
    reason="API integration tests require running server - skipped in CI",
)
class TestResearchAPI:
    """Test suite for research API endpoints"""

    def test_research_without_required_fields(self, auth_session):
        """Test validation of required fields"""
        session, csrf_token = auth_session

        invalid_requests = [
            {},  # Empty request
            {"model": "gemma3n:e2b"},  # Missing query
            {"query": "test"},  # Missing model
            {"query": "", "model": "gemma3n:e2b"},  # Empty query
            {"query": "test", "model": ""},  # Empty model
        ]

        for data in invalid_requests:
            response = session.post(f"{BASE_URL}/api/start_research", json=data)

            assert response.status_code in [400, 422], (
                f"Expected validation error for {data}, got {response.status_code}"
            )
            print(f"✓ Correctly rejected invalid request: {data}")


if __name__ == "__main__":
    # Run tests with verbose output
    pytest.main([__file__, "-v", "-s"])
