#!/usr/bin/env python
"""Compile .po files to .mo files for Flask-Babel."""
from pathlib import Path
from babel.messages.mofile import write_mo
from babel.messages.pofile import read_po

def compile_po_file(po_path: Path) -> None:
    """Compile a single .po file to .mo."""
    mo_path = po_path.with_suffix('.mo')

    print(f"Compiling {po_path} -> {mo_path}")

    with open(po_path, 'rb') as po_file:
        catalog = read_po(po_file)

    with open(mo_path, 'wb') as mo_file:
        write_mo(mo_file, catalog)

    print(f"  {len(catalog)} messages compiled")

def main():
    """Find and compile all .po files in translations directory."""
    translations_dir = Path(__file__).parent / 'translations'

    if not translations_dir.exists():
        print(f"ERROR: {translations_dir} does not exist")
        return

    po_files = list(translations_dir.rglob('*.po'))

    if not po_files:
        print(f"No .po files found in {translations_dir}")
        return

    print(f"Found {len(po_files)} .po file(s)")

    for po_file in po_files:
        compile_po_file(po_file)

    print("\n✓ All translations compiled successfully")

if __name__ == '__main__':
    main()
