# tests/test_integration.py
"""
Integration tests for juiceKEGGers
"""
import pytest
import subprocess
import sys
from pathlib import Path


def test_full_workflow_integration(temp_dir):
    """Test a complete workflow with test data."""
    # This is a lightweight integration test using small test files
    
    # 1. Test download-org (mock version)
    result = subprocess.run(
        [sys.executable, "-m", "juicekeggers.cli", "info"],
        capture_output=True,
        text=True
    )
    assert result.returncode == 0
    
    # 2. Test parsing with test data
    from juicekeggers.utils.parsers import parse_org_file
    
    # Create a test organisms file
    test_orgs = temp_dir / "test_orgs.tsv"
    test_orgs.write_text("""test1\tOrganism 1\thttp://example.com
test2\tOrganism 2\thttp://example.com
""")
    
    parsed = parse_org_file(test_orgs)
    assert len(parsed) == 2
    assert parsed[0]["org_code"] == "test1"


def test_make_keg_integration(temp_dir, sample_keg_file, sample_annotations_tsv):
    """Test make-keg command integration."""
    # Copy test files to temp dir
    import shutil
    shutil.copy(sample_keg_file, temp_dir / "template.keg")
    shutil.copy(sample_annotations_tsv, temp_dir / "annotations.tsv")
    
    # Run make-keg command via Python module
    from juicekeggers.commands.make_keg import create_keg_file
    
    result = create_keg_file(
        keg_template=str(temp_dir / "template.keg"),
        input_file=str(temp_dir / "annotations.tsv"),
        output_prefix=str(temp_dir / "output")
    )
    
    assert result == 0
    
    # Check output file was created
    output_file = temp_dir / "output.keg"
    assert output_file.exists()
    
    # Check it has content
    content = output_file.read_text()
    assert "K00001" in content
    assert "NP_000001.1" in content


@pytest.mark.slow
def test_end_to_end_workflow(temp_dir):
    """End-to-end test with mocked downloads."""
    # This would mock network calls and test the full pipeline
    # Skip in normal test runs
    
    # 1. Mock download-org
    # 2. Mock download-ko  
    # 3. Mock download-proteins
    # 4. Run make-db
    # 5. Verify output
    
    pass  # Implementation would use extensive mocking
    