"""Smoke tests for the Typer CLI (HTTP mocked with `responses`)."""

import pytest
import responses

typer = pytest.importorskip("typer")
from tests.conftest import BASE, FAKE_TOKEN, load_fixture  # noqa: E402
from typer.testing import CliRunner  # noqa: E402

from accuweather_client.cli import app  # noqa: E402

runner = CliRunner()


def mock_location():
    responses.get(
        f"{BASE}/locations/v1/cities/search",
        json=load_fixture("location_city.json"),
    )


@responses.activate
def test_cli_now():
    mock_location()
    responses.get(
        f"{BASE}/currentconditions/v1/22889",
        json=load_fixture("current_conditions.json"),
    )
    result = runner.invoke(
        app, ["now", "--city", "sydney", "--token", FAKE_TOKEN]
    )
    assert result.exit_code == 0
    assert "sunny" in result.output


@responses.activate
def test_cli_forecast_table():
    mock_location()
    responses.get(
        f"{BASE}/forecasts/v1/daily/5day/22889",
        json=load_fixture("forecast_5day.json"),
    )
    result = runner.invoke(
        app, ["forecast", "--city", "sydney", "--token", FAKE_TOKEN]
    )
    assert result.exit_code == 0
    assert "Partly sunny" in result.output


def test_cli_requires_token(monkeypatch, tmp_path):
    monkeypatch.delenv("ACCUWEATHER_API_KEY", raising=False)
    monkeypatch.delenv("API_TOKEN", raising=False)
    # Run outside the repo so a developer's real .env file is not picked up.
    monkeypatch.chdir(tmp_path)
    result = runner.invoke(app, ["now", "--city", "sydney"])
    assert result.exit_code == 2
    assert "No API key" in result.output


def test_cli_version():
    from accuweather_client import __version__

    result = runner.invoke(app, ["version"])
    assert result.exit_code == 0
    assert __version__ in result.output
