"""Tests for ``GET /api/admin/validate-provider`` (SP20).

Pure read-only endpoint — runs the local NPI Luhn + EIN format checks.
"""
from __future__ import annotations

import pytest
from fastapi.testclient import TestClient

from cyclone.api import app


@pytest.fixture
def client() -> TestClient:
    return TestClient(app)


# ---------------------------------------------------------------------------
# Both fields populated
# ---------------------------------------------------------------------------


def test_validate_provider_both_valid(client: TestClient):
    resp = client.get("/api/admin/validate-provider", params={
        "npi": "1234567893",  # CMS-published valid NPI
        "tax_id": "72-1587149",  # Touch of Care EIN
    })
    assert resp.status_code == 200
    body = resp.json()
    assert body["npi"]["valid"] is True
    assert body["npi"]["skipped"] is False
    assert body["tax_id"]["valid"] is True
    assert body["tax_id"]["normalized"] == "721587149"


def test_validate_provider_both_invalid(client: TestClient):
    resp = client.get("/api/admin/validate-provider", params={
        "npi": "1234567890",  # format OK but Luhn fails
        "tax_id": "00-1234567",  # reserved prefix
    })
    assert resp.status_code == 200
    body = resp.json()
    assert body["npi"]["valid"] is False
    assert body["tax_id"]["valid"] is False
    assert body["tax_id"]["normalized"] == "001234567"


# ---------------------------------------------------------------------------
# Param omission → skipped
# ---------------------------------------------------------------------------


def test_validate_provider_skips_missing_npi(client: TestClient):
    resp = client.get("/api/admin/validate-provider", params={"tax_id": "721587149"})
    assert resp.status_code == 200
    body = resp.json()
    assert body["npi"]["skipped"] is True
    assert body["npi"]["valid"] is None
    assert body["tax_id"]["valid"] is True


def test_validate_provider_skips_missing_tax_id(client: TestClient):
    resp = client.get("/api/admin/validate-provider", params={"npi": "1234567893"})
    assert resp.status_code == 200
    body = resp.json()
    assert body["tax_id"]["skipped"] is True
    assert body["tax_id"]["valid"] is None
    assert body["tax_id"]["normalized"] is None
    assert body["npi"]["valid"] is True