#!/usr/bin/env python3
"""Block commits with substantial new code but no tests.

Exits 1 (blocks commit) when zero test files are staged AND at least one of:
  - A new source file has >50 added lines (real new functionality)
  - >300 total new lines across source files (substantial change)

Otherwise exits 0 silently.
"""

import sys
from pathlib import Path

# Allow importing sibling module
sys.path.insert(0, str(Path(__file__).resolve().parent))

from _commit_analysis import analyze_commit, suggest_test_path

# Thresholds
NEW_FILE_LINE_THRESHOLD = 50
TOTAL_LINES_THRESHOLD = 300


def main():
    analysis = analyze_commit()

    # Pass: no source files
    if not analysis.source_files:
        return 0

    # Pass: tests are included
    if analysis.has_tests:
        return 0

    # Check blocking conditions
    large_new_files = [
        f
        for f in analysis.new_source_files
        if f.added > NEW_FILE_LINE_THRESHOLD
    ]
    substantial_total = analysis.total_source_added > TOTAL_LINES_THRESHOLD

    if not large_new_files and not substantial_total:
        return 0

    # Block: print reason and suggestions
    print()
    print("  \033[31mTests Required\033[0m")
    print("  " + "=" * 40)

    if large_new_files:
        print()
        print("  New source files with significant code:")
        for f in large_new_files:
            suggested = suggest_test_path(f.path)
            print(f"    {f.path} (+{f.added} lines)")
            print(f"      -> {suggested}")

    if substantial_total:
        print()
        print(
            f"  Total new source lines: {analysis.total_source_added} "
            f"(threshold: {TOTAL_LINES_THRESHOLD})"
        )

    print()
    print("  Please add tests before committing.")
    print()

    return 1


if __name__ == "__main__":
    sys.exit(main())
