# tests/test_c18_cli_init.py
from __future__ import annotations

from pathlib import Path

import pytest

from veritell_langchain.cli import main as veritell_main
from veritell_langchain.onboarding import redact_secret


def test_c18_redact_secret_does_not_leak() -> None:
    assert redact_secret(None) == "<unset>"
    assert redact_secret("") == "<unset>"
    assert redact_secret("abcd") == "****"
    assert redact_secret("sk_test_1234567890") == "sk_test_****7890"


def test_c18_default_config_path_is_appdata_on_windows(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    monkeypatch.setenv("APPDATA", str(tmp_path))
    from veritell_langchain.onboarding import default_config_path

    p = default_config_path()
    assert str(p).endswith(r"\veritell\veritell.toml")
    assert str(tmp_path) in str(p)


def test_c18_init_writes_config_from_env_vars(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    monkeypatch.setenv("APPDATA", str(tmp_path))
    monkeypatch.setenv("VERITELL_API_KEY_STAGING", "stg_key_1234")
    monkeypatch.setenv("VERITELL_API_KEY_PRODUCTION", "prod_key_5678")
    monkeypatch.setenv("VERITELL_BASE_URL_STAGING", "https://api.dev.veritell.ai")
    monkeypatch.setenv("VERITELL_BASE_URL_PRODUCTION", "https://api.veritell.ai")

    exit_code = veritell_main(["init"])
    assert exit_code == 0

    cfg_path = tmp_path / "veritell" / "veritell.toml"
    assert cfg_path.exists()
    txt = cfg_path.read_text(encoding="utf-8")
    assert 'env = "production"' in txt
    assert 'api_key = "stg_key_1234"' in txt
    assert 'api_key = "prod_key_5678"' in txt
    assert 'base_url = "https://api.dev.veritell.ai"' in txt
    assert 'base_url = "https://api.veritell.ai"' in txt


def test_c18_init_prompts_when_env_vars_missing(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
    monkeypatch.setenv("APPDATA", str(tmp_path))
    monkeypatch.delenv("VERITELL_API_KEY_STAGING", raising=False)
    monkeypatch.delenv("VERITELL_API_KEY_PRODUCTION", raising=False)

    # prompt inputs (secure, so via getpass)
    import veritell_langchain.onboarding as onboarding

    answers = iter(["stg_from_prompt", "prod_from_prompt"])
    monkeypatch.setattr(onboarding, "_prompt_secret", lambda prompt: next(answers))

    exit_code = veritell_main(["init", "--no-browser"])
    assert exit_code == 0

    cfg_path = tmp_path / "veritell" / "veritell.toml"
    txt = cfg_path.read_text(encoding="utf-8")
    assert 'api_key = "stg_from_prompt"' in txt
    assert 'api_key = "prod_from_prompt"' in txt

    out = capsys.readouterr().out
    assert "stg_from_prompt" not in out
    assert "prod_from_prompt" not in out


def test_c18_init_print_flag_requires_explicit_opt_in(
    monkeypatch: pytest.MonkeyPatch, tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
    monkeypatch.setenv("APPDATA", str(tmp_path))
    monkeypatch.setenv("VERITELL_API_KEY_STAGING", "stg_key_print")
    monkeypatch.setenv("VERITELL_API_KEY_PRODUCTION", "prod_key_print")

    exit_code = veritell_main(["init", "--print"])
    assert exit_code == 0

    out = capsys.readouterr().out
    assert "WARNING" in out
    assert "stg_key_print" in out
    assert "prod_key_print" in out
