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

client = TestClient(app)

@patch("app.routes.contact.fetch_repos", return_value=[])
@patch("app.routes.contact.send_contact_email", new_callable=AsyncMock)
@patch("app.routes.contact.create_contact", new_callable=AsyncMock)
@patch("app.routes.contact.httpx.AsyncClient.post")
def test_full_contact_submission(mock_recaptcha, mock_create, mock_send, mock_fetch):
    # Mock reCAPTCHA verification
    mock_resp = AsyncMock()
    mock_resp.json.return_value = {"success": True}
    mock_recaptcha.return_value = mock_resp

    response = client.post("/contact", json={
        "name": "Jean Dupont",
        "email": "jean@example.com",
        "subject": "Test contact",
        "message": "Hello, I'd like to discuss a project.",
        "recaptcha_token": "valid-token"
    })

    assert response.status_code == 200
    assert response.json()["success"] is True
    mock_create.assert_awaited_once()
    mock_send.assert_awaited_once()

@patch("app.routes.contact.fetch_repos", return_value=[])
def test_contact_page_contains_form_elements(_):
    response = client.get("/contact")
    assert response.status_code == 200
    assert '<form' in response.text
    assert 'g-recaptcha' in response.text
    assert 'name="name"' in response.text
    assert 'name="email"' in response.text
