import pytest
import httpx
import wave
import io

@pytest.mark.asyncio
async def test_root_endpoint(http_client):
    """
    Tests if the root endpoint is alive and returns the correct status message.
    """
    print("\n--- Running test_root_endpoint ---")
    response = await http_client.get("/")
    assert response.status_code == 200
    assert response.json() == {"status": "AI Model Hub is running!"}
    print("✅ Root endpoint test passed.")

@pytest.mark.asyncio
async def test_create_speech_stream(http_client):
    """
    Tests the /speech endpoint for a successful audio stream response.
    """
    print("\n--- Running test_create_speech_stream ---")
    url = "/speech"
    payload = {"text": "Hello, world!"}

    # The `stream=True` parameter tells httpx to not read the entire response body
    # at once. We'll handle it manually to check for content.
    async with http_client.stream("POST", url, json=payload) as response:
        assert response.status_code == 200, f"Speech stream request failed. Response: {response.text}"
        assert response.headers.get("content-type") == "audio/wav"

        # Check that the response body is not empty by iterating over chunks.
        content_length = 0
        async for chunk in response.aiter_bytes():
            content_length += len(chunk)

        assert content_length > 0
    print("✅ TTS stream test passed.")

@pytest.mark.asyncio
async def test_stt_transcribe_endpoint(http_client):
    """
    Tests the /stt/transcribe endpoint by uploading a dummy audio file
    and verifying the transcription response.
    """
    print("\n--- Running test_stt_transcribe_endpoint ---")
    url = "/stt/transcribe"

    # --- Use a real audio file from the integration test data ---
    audio_file_path = "integration_tests/test_data/test-audio.wav"
    
    with open(audio_file_path, "rb") as audio_file:
        files = {'audio_file': ('test-audio.wav', audio_file, 'audio/wav')}
    
        # --- Send the POST request to the endpoint ---
        response = await http_client.post(url, files=files)
    
    # --- Assertions ---
    assert response.status_code == 200, f"STT request failed with status code {response.status_code}. Response: {response.text}"
    response_json = response.json()
    assert "transcript" in response_json, "Response JSON is missing the 'transcript' key."
    assert isinstance(response_json["transcript"], str), "Transcript value is not a string."
    
    # Assert that the transcript matches the expected text
    expected_transcript = "This audio is for integration testing of Cortex Hub, which is a wonderful project."
    assert response_json["transcript"] == expected_transcript, f"Expected: '{expected_transcript}', Got: '{response_json['transcript']}'"
    
    print("✅ STT transcription test passed.")

