"""Tests for ``cyclone validate-npi`` + ``cyclone validate-tax-id`` (SP20).

CLI smoke tests — verify exit codes (0 = valid, 1 = invalid) and that
the help text references the new subcommands. We don't pipe the value
into shared logs (NPI / EIN are PHI / PII); the CliRunner captures it.
"""
from __future__ import annotations

import pytest
from click.testing import CliRunner

from cyclone.cli import main


# ---------------------------------------------------------------------------
# validate-npi
# ---------------------------------------------------------------------------


def test_cli_validate_npi_valid_exits_zero():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-npi", "1234567893"])
    assert result.exit_code == 0, result.output
    assert "OK" in result.output


def test_cli_validate_npi_bad_checksum_exits_one():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-npi", "1234567890"])
    assert result.exit_code == 1
    assert "INVALID" in result.output


def test_cli_validate_npi_wrong_length_exits_one():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-npi", "12345"])
    assert result.exit_code == 1
    assert "INVALID" in result.output


# ---------------------------------------------------------------------------
# validate-tax-id
# ---------------------------------------------------------------------------


def test_cli_validate_tax_id_formatted_exits_zero():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-tax-id", "72-1587149"])
    assert result.exit_code == 0, result.output
    assert "721587149" in result.output  # normalized form echoed


def test_cli_validate_tax_id_unformatted_exits_zero():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-tax-id", "721587149"])
    assert result.exit_code == 0


def test_cli_validate_tax_id_reserved_prefix_exits_one():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-tax-id", "00-1234567"])
    assert result.exit_code == 1
    assert "reserved" in result.output.lower()


def test_cli_validate_tax_id_malformed_exits_one():
    runner = CliRunner()
    result = runner.invoke(main, ["validate-tax-id", "not-an-ein"])
    assert result.exit_code == 1
    assert "9-digit" in result.output


def test_cli_validate_subcommands_appear_in_help():
    """The two new subcommands are wired into ``main`` (regression guard
    against future refactors that drop the imports)."""
    runner = CliRunner()
    result = runner.invoke(main, ["--help"])
    assert result.exit_code == 0
    assert "validate-npi" in result.output
    assert "validate-tax-id" in result.output