#!/usr/bin/python3
"""
Compare msgid entries between two PO files.
Usage:
  mint-compare-potfiles                 # Auto-compare current .pot with HEAD version
  mint-compare-potfiles file1.po file2.po  # Compare two specific files
"""

import sys
import re
import os
import subprocess
import tempfile
import glob


def extract_msgids(filename):
    """
    Extract all msgid strings and their line numbers from a file.
    Returns a dictionary: {msgid_string: line_number}
    """
    msgids = {}
    pattern = re.compile(r'^\s*msgid\s+"(.*)"\s*$')

    try:
        with open(filename, 'r', encoding='utf-8') as f:
            for line_num, line in enumerate(f, start=1):
                match = pattern.match(line)
                if match:
                    msgid_text = match.group(1)
                    # Only store non-empty msgids (skip the header entry)
                    if msgid_text:
                        msgids[msgid_text] = line_num
    except FileNotFoundError:
        print(f"Error: File '{filename}' not found.", file=sys.stderr)
        sys.exit(1)
    except Exception as e:
        print(f"Error reading '{filename}': {e}", file=sys.stderr)
        sys.exit(1)

    return msgids


def find_potfile():
    pot_files = glob.glob("*.pot")
    if not pot_files:
        return None
    return pot_files[0]

def get_potfile_from_git(filepath):
    try:
        result = subprocess.run(
            ['git', 'show', f'HEAD:{filepath}'],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout
    except subprocess.CalledProcessError:
        return None


def main():
    if len(sys.argv) == 1:
        potfile = find_potfile()
        if not potfile:
            print("Error: No .pot file found in current directory.", file=sys.stderr)
            sys.exit(1)

        head_content = get_potfile_from_git(potfile)
        if head_content is None:
            print(f"Error: Could not get '{potfile}' from git HEAD.", file=sys.stderr)
            print("Make sure you're in a git repository and the file exists in HEAD.", file=sys.stderr)
            sys.exit(1)

        with tempfile.NamedTemporaryFile(mode='w', suffix='.pot', delete=False, encoding='utf-8') as temp_file:
            temp_file.write(head_content)
            temp_file_path = temp_file.name

        msgids1 = extract_msgids(temp_file_path)
        msgids2 = extract_msgids(potfile)

        file1 = f"{potfile} (HEAD)"
        file2 = f"{potfile} (current)"

    elif len(sys.argv) == 3:
        file1 = sys.argv[1]
        file2 = sys.argv[2]

        # Extract msgids from both files
        msgids1 = extract_msgids(file1)
        msgids2 = extract_msgids(file2)

    else:
        print("Usage: mint-compare-potfiles [file1.po file2.po]", file=sys.stderr)
        print("  With no arguments: compares current .pot file with HEAD version", file=sys.stderr)
        print("  With two arguments: compares the two specified files", file=sys.stderr)
        sys.exit(1)

    only_in_file1 = set(msgids1.keys()) - set(msgids2.keys())
    only_in_file2 = set(msgids2.keys()) - set(msgids1.keys())

    if only_in_file1:
        print(f"=== msgid entries only in {file1} ===")
        for msgid in sorted(only_in_file1):
            print(f"Line {msgids1[msgid]}: \"{msgid}\"")
        print()
    
    if only_in_file2:
        print(f"=== msgid entries only in {file2} ===")
        for msgid in sorted(only_in_file2):
            print(f"Line {msgids2[msgid]}: \"{msgid}\"")
        print()
    
    if not only_in_file1 and not only_in_file2:
        print("No differences found. Both files have the same msgid entries.")


if __name__ == "__main__":
    main()