"""Tests for POST /system/jira-test's diagnostic message.

Regression coverage for a real QA-reported bug: has_admin_jira_configured()
bundles four independent checks (enabled, url, admin_email, admin_api_token)
into one bool, but the error message always blamed "Admin Email and Admin
API Token" regardless of which check actually failed. An admin who had set
url/email/token correctly but never flipped the "Enable Jira Integration"
toggle saw a misleading message pointing at the wrong fields.
"""

from app.models.system_config import SystemConfig


def _set_config(db, key, value):
    db.add(SystemConfig(key=key, value=value))


def test_jira_test_reports_disabled_toggle_specifically(client, db, auth_headers):
    """url/email/token all set, but jira.enabled is missing entirely —
    the message must call out the toggle, not email/token."""
    _set_config(db, "jira.url", "https://example.atlassian.net")
    _set_config(db, "jira.admin_email", "admin@example.com")
    _set_config(db, "jira.admin_api_token", "some-real-token")
    db.commit()

    resp = client.post("/api/v1/system/jira-test", headers=auth_headers)
    assert resp.status_code == 200
    body = resp.json()
    assert body["status"] == "error"
    assert "not enabled" in body["message"]
    assert "Admin Email" not in body["message"]
    assert "Admin API Token" not in body["message"]


def test_jira_test_reports_missing_email_and_token_when_actually_missing(client, db, auth_headers):
    _set_config(db, "jira.url", "https://example.atlassian.net")
    _set_config(db, "jira.enabled", "true")
    db.commit()

    resp = client.post("/api/v1/system/jira-test", headers=auth_headers)
    assert resp.status_code == 200
    body = resp.json()
    assert body["status"] == "error"
    assert "Admin Email is not set" in body["message"]
    assert "Admin API Token is not set" in body["message"]
    assert "not enabled" not in body["message"]


def test_jira_test_missing_url_reports_url_specifically(client, db, auth_headers):
    resp = client.post("/api/v1/system/jira-test", headers=auth_headers)
    assert resp.status_code == 200
    body = resp.json()
    assert body["status"] == "error"
    assert "Jira URL is not configured" in body["message"]
