"""
Tests for varbook CLI interface.
"""

import pytest
import subprocess
import sys
from pathlib import Path


def test_varbook_help():
    """Test that varbook --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'varbook' in result.stdout.lower()
    assert 'annotate' in result.stdout
    assert 'plot' in result.stdout
    assert 'write' in result.stdout


def test_varbook_annotate_help():
    """Test that varbook annotate --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'annotate', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'kmeans' in result.stdout


def test_varbook_plot_help():
    """Test that varbook plot --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'plot', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'models' in result.stdout or 'variant' in result.stdout


def test_varbook_write_help():
    """Test that varbook write --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'write', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'pdf' in result.stdout


def test_varbook_annotate_kmeans_help():
    """Test that varbook annotate kmeans --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'annotate', 'kmeans', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'input_tsv' in result.stdout or 'INPUT' in result.stdout
    assert 'datasets' in result.stdout or 'models' in result.stdout


def test_varbook_plot_variant_model_scatterplot_help():
    """Test that varbook plot variant model-scatterplot --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'plot', 'variant', 'model-scatterplot', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0


def test_varbook_write_pdf_help():
    """Test that varbook write pdf --help works."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook', 'write', 'pdf', '--help'],
        capture_output=True,
        text=True
    )

    assert result.returncode == 0
    assert 'output' in result.stdout.lower()
    assert 'beginning-files' in result.stdout or 'variant-files' in result.stdout


def test_varbook_no_args():
    """Test that varbook with no arguments shows help."""
    result = subprocess.run(
        [sys.executable, '-m', 'varbook'],
        capture_output=True,
        text=True
    )

    # Should show error or help
    assert result.returncode != 0 or 'usage' in result.stdout.lower()
