#!/usr/bin/env python3
"""Show Rust codebase statistics."""

import subprocess
from pathlib import Path


def count_lines(paths: list[Path]) -> int:
    """Count total lines in files."""
    total = 0
    for path in paths:
        if path.is_file():
            total += sum(1 for _ in path.open())
    return total


def find_rust_files(directory: Path, exclude: list[str] = None) -> list[Path]:
    """Find all .rs files in directory, excluding certain paths."""
    exclude = exclude or []
    files = []
    for f in directory.rglob("*.rs"):
        if not any(ex in str(f) for ex in exclude):
            files.append(f)
    return files


def main():
    root = Path(__file__).parent.parent

    # Find files in different categories
    src_files = find_rust_files(root / "src")
    test_files = find_rust_files(root / "tests")
    bench_files = find_rust_files(root / "benches") if (root / "benches").exists() else []
    fuzz_files = find_rust_files(root / "fuzz") if (root / "fuzz").exists() else []
    old_impl_files = find_rust_files(root / ".old-implementation") if (root / ".old-implementation").exists() else []
    plugin_files = find_rust_files(root / "plugins") if (root / "plugins").exists() else []

    # Count lines
    src_lines = count_lines(src_files)
    test_lines = count_lines(test_files)
    bench_lines = count_lines(bench_files)
    fuzz_lines = count_lines(fuzz_files)
    old_impl_lines = count_lines(old_impl_files)
    plugin_lines = count_lines(plugin_files)

    # Generated tests file
    generated_tests = root / "tests" / "generated_syntax_tests.rs"
    generated_lines = count_lines([generated_tests]) if generated_tests.exists() else 0
    handwritten_test_lines = test_lines - generated_lines

    # Root level .rs files (build.rs, etc.)
    root_rs_files = list(root.glob("*.rs"))
    root_lines = count_lines(root_rs_files)

    # Total excluding target/
    all_files = find_rust_files(root, exclude=["target/"])
    total_lines = count_lines(all_files)

    # Print table
    print()
    print("┌─────────────────────────────────┬──────────┐")
    print("│ Category                        │    Lines │")
    print("├─────────────────────────────────┼──────────┤")
    print(f"│ src/ (main code)                │ {src_lines:>8,} │")
    print(f"│ tests/                          │ {test_lines:>8,} │")
    print(f"│   └─ generated_syntax_tests.rs  │ {generated_lines:>8,} │")
    print(f"│   └─ hand-written tests         │ {handwritten_test_lines:>8,} │")
    if bench_lines:
        print(f"│ benches/                        │ {bench_lines:>8,} │")
    if fuzz_lines:
        print(f"│ fuzz/                           │ {fuzz_lines:>8,} │")
    if plugin_lines:
        print(f"│ plugins/                        │ {plugin_lines:>8,} │")
    if old_impl_lines:
        print(f"│ .old-implementation/            │ {old_impl_lines:>8,} │")
    if root_lines:
        print(f"│ root (build.rs, etc.)           │ {root_lines:>8,} │")
    print("├─────────────────────────────────┼──────────┤")
    print(f"│ Total (excl target/)            │ {total_lines:>8,} │")
    print("└─────────────────────────────────┴──────────┘")
    print()

    # File counts
    print(f"Files: {len(all_files)} .rs files")
    print()


if __name__ == "__main__":
    main()
