from fastapi.testclient import TestClient
from app.main import app
from unittest.mock import patch, AsyncMock, MagicMock

client = TestClient(app)

def test_contact_page_loads():
    response = client.get("/contact")
    assert response.status_code == 200
    assert "Contact" in response.text
    assert "g-recaptcha" in response.text

@patch("app.routes.contact.create_contact", new_callable=AsyncMock)
@patch("app.routes.contact.send_contact_email", new_callable=AsyncMock)
@patch("app.routes.contact.httpx.AsyncClient.post")
def test_contact_post_success(mock_recaptcha_post, mock_send, mock_create):
    # Mock the response from reCAPTCHA verification
    mock_response = MagicMock()
    mock_response.json.return_value = {"success": True}
    # AsyncClient.post returns (await) the response
    mock_recaptcha_post.return_value = mock_response

    response = client.post("/contact", json={
        "name": "Test",
        "email": "test@example.com",
        "subject": "Hello",
        "message": "World",
        "recaptcha_token": "fake-token"
    })
    assert response.status_code == 200
    json = response.json()
    assert json["success"] is True

def test_contact_post_invalid_data():
    response = client.post("/contact", json={
        "name": "",
        "email": "bad-email",
        "subject": "Test",
        "message": "Hello",
        "recaptcha_token": "fake-token"
    })
    assert response.status_code == 422  # Validation error
