#!/usr/bin/env python3
"""
Simple coverage runner for Coverage Gutters extension.
Generates coverage.xml file needed by the VSCode Coverage Gutters extension.
Uses the same approach as CI for consistency.
"""

import subprocess
import sys
import os


def main():
    """Run coverage for Coverage Gutters."""
    print("Generating coverage for Coverage Gutters...")
    print("Using the same pytest approach as CI...")

    try:
        # Run tests with coverage and generate all report formats (same as CI)
        cmd = [
            sys.executable, "-m", "pytest",
            "tests/",
            "-v",
            "--cov=pyWebLayout",
            "--cov-report=term-missing",
            "--cov-report=json",
            "--cov-report=html",
            "--cov-report=xml"
        ]

        print(f"Running: {' '.join(cmd)}")
        _result = subprocess.run(cmd, check=True)

        # Check if coverage.xml was created
        if os.path.exists("coverage.xml"):
            print("✓ coverage.xml generated successfully!")
            print("✓ coverage.json generated for CI compatibility")
            print("✓ HTML coverage report generated in htmlcov/")
            print("\nCoverage Gutters should now be able to display coverage data.")
            print("\nTo use Coverage Gutters in VSCode:")
            print("1. Open Command Palette (Ctrl+Shift+P)")
            print("2. Run 'Coverage Gutters: Remove Coverage' (to clear cache)")
            print("3. Run 'Coverage Gutters: Display Coverage'")
            print("4. Or use the Coverage Gutters buttons in the status bar")

            # Show file info
            size = os.path.getsize("coverage.xml")
            print(f"\nGenerated coverage.xml: {size} bytes")

        else:
            print("✗ coverage.xml was not generated")

    except subprocess.CalledProcessError as e:
        print(f"Error running tests: {e}")
        print("This may indicate test failures or missing dependencies.")
        sys.exit(1)
    except FileNotFoundError:
        print("pytest not found. Please install it with: pip install pytest pytest-cov")
        sys.exit(1)


if __name__ == "__main__":
    main()
