#!/usr/bin/env python3
"""
Pre-commit hook to prevent usage of ldr.db (shared database).
All data should be stored in per-user encrypted databases.
"""

import sys
import re
import os
from pathlib import Path

# Set environment variable for pre-commit hooks to allow unencrypted databases
os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"


def check_file_for_ldr_db(file_path):
    """Check if a file contains references to ldr.db."""
    try:
        with open(file_path, "r", encoding="utf-8") as f:
            content = f.read()
    except (UnicodeDecodeError, IOError):
        # Skip binary files or files we can't read
        return []

    # Pattern to find ldr.db references
    pattern = r"ldr\.db"
    matches = []

    for line_num, line in enumerate(content.splitlines(), 1):
        if re.search(pattern, line, re.IGNORECASE):
            # Skip comments and documentation
            stripped = line.strip()
            if not (
                stripped.startswith("#")
                or stripped.startswith("//")
                or stripped.startswith("*")
                or stripped.startswith('"""')
                or stripped.startswith("'''")
            ):
                matches.append((line_num, line.strip()))

    return matches


def main():
    """Main function to check all Python files for ldr.db usage."""
    # Get all Python files from command line arguments
    files_to_check = sys.argv[1:] if len(sys.argv) > 1 else []

    if not files_to_check:
        # If no files specified, check all Python files
        src_dir = Path(__file__).parent.parent / "src"
        files_to_check = list(src_dir.rglob("*.py"))

    violations = []

    for file_path in files_to_check:
        file_path = Path(file_path)

        # Only skip this hook file itself
        if file_path.name == "check-ldr-db.py":
            continue

        matches = check_file_for_ldr_db(file_path)
        if matches:
            violations.append((file_path, matches))

    if violations:
        print("❌ DEPRECATED ldr.db USAGE DETECTED!")
        print("=" * 60)
        print("The shared ldr.db database is deprecated.")
        print("All data must be stored in per-user encrypted databases.")
        print("=" * 60)

        for file_path, matches in violations:
            print(f"\n📄 {file_path}")
            for line_num, line in matches:
                print(f"   Line {line_num}: {line}")

        print("\n" + "=" * 60)
        print("MIGRATION REQUIRED:")
        print("1. Store user-specific data in encrypted per-user databases")
        print("2. Use get_user_db_session() instead of shared database access")
        print("3. See migration guide in documentation")
        print("=" * 60)

        return 1

    return 0


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