"""Smoke tests for the CLI entrypoint.

Exercises the CLI commands end-to-end against live services.
Requires FITBIT_CLIENT_ID, FITBIT_CLIENT_SECRET, and a valid
.fitbit_tokens.json.
"""

import json
import os
import subprocess
import sys

import pytest


pytestmark = pytest.mark.skipif(
    not os.environ.get("FITBIT_CLIENT_ID"),
    reason="FITBIT_CLIENT_ID not set",
)


class TestSyncCommand:
    def test_fitbit_sync(self, tmp_path):
        result = subprocess.run(
            [
                sys.executable,
                "-m", "byteb4rb1e.homeostat",
                "-v", "sync",
                "--source", "fitbit",
                "--data-dir", str(tmp_path / "data"),
            ],
            capture_output=True,
            text=True,
            timeout=60,
        )
        assert result.returncode == 0, result.stderr
        assert "Fitbit sync complete" in result.stderr

        # Verify canonical data was written
        data_dir = tmp_path / "data"
        step_files = list(
            (data_dir / "step-snapshot").glob("*.jsonl")
        )
        assert len(step_files) >= 1

    def test_fddb_sync(self, tmp_path):
        if not os.environ.get("FDDB_USERNAME"):
            pytest.skip("FDDB_USERNAME not set")

        result = subprocess.run(
            [
                sys.executable,
                "-m", "byteb4rb1e.homeostat",
                "-v", "sync",
                "--source", "fddb",
                "--data-dir", str(tmp_path / "data"),
            ],
            capture_output=True,
            text=True,
            timeout=60,
        )
        assert result.returncode == 0, result.stderr


class TestProjectCommand:
    def test_project_after_sync(self, tmp_path):
        data_dir = tmp_path / "data"
        build_dir = tmp_path / "build"

        # Sync first
        subprocess.run(
            [
                sys.executable,
                "-m", "byteb4rb1e.homeostat",
                "sync",
                "--source", "fitbit",
                "--data-dir", str(data_dir),
            ],
            capture_output=True,
            timeout=60,
        )

        # Project
        result = subprocess.run(
            [
                sys.executable,
                "-m", "byteb4rb1e.homeostat",
                "-v", "project",
                "--data-dir", str(data_dir),
                "--build-dir", str(build_dir),
            ],
            capture_output=True,
            text=True,
            timeout=30,
        )
        assert result.returncode == 0, result.stderr
        assert "Projection built" in result.stderr

        current = build_dir / "projection" / "current"
        assert current.is_symlink()

        status = json.loads(
            (current / "status.json").read_text()
        )
        assert "vitals" in status
        assert status["vitals"]["steps_today"] >= 0


class TestHelpOutput:
    def test_root_help(self):
        result = subprocess.run(
            [
                sys.executable,
                "-m", "byteb4rb1e.homeostat",
                "--help",
            ],
            capture_output=True,
            text=True,
            timeout=10,
        )
        assert result.returncode == 0
        assert "authorize" in result.stdout
        assert "sync" in result.stdout
        assert "project" in result.stdout
