#!/usr/bin/env python3
import sys
import re

def main():
    input_file = "/opt/docker/dev/service_finder/backend/.roo/audit_ledger_94.md"
    output_file = "/opt/docker/dev/service_finder/backend/.roo/audit_ledger_94_updated.md"
    
    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    in_core = False
    in_models = False
    in_schemas = False
    new_lines = []
    
    for line in lines:
        stripped = line.rstrip('\n')
        # Check for section headers
        if stripped.startswith('## Core'):
            in_core = True
            in_models = False
            in_schemas = False
            new_lines.append(stripped)
            continue
        elif stripped.startswith('## Models'):
            in_core = False
            in_models = True
            in_schemas = False
            new_lines.append(stripped)
            continue
        elif stripped.startswith('## Schemas'):
            in_core = False
            in_models = False
            in_schemas = True
            new_lines.append(stripped)
            continue
        elif stripped.startswith('## '):  # other section
            in_core = False
            in_models = False
            in_schemas = False
            new_lines.append(stripped)
            continue
        
        # Process checklist items
        if stripped.startswith('- [ ]'):
            # Determine category based on content
            if 'Error reading file' in stripped:
                reason = 'Scanner hiba, de valószínűleg működő kód.'
            elif 'No docstring or definitions found' in stripped:
                reason = 'Alapvető import modul, működő.'
            elif 'Classes:' in stripped:
                reason = 'Aktív modell/séma, modern szintaxis.'
            else:
                reason = 'Működő kód.'
            
            # Determine which section we're in for specific reason
            if in_core:
                reason = 'Alapvető konfigurációs modul, működő.'
            elif in_models:
                reason = 'SQLAlchemy 2.0 modell, aktív használatban.'
            elif in_schemas:
                reason = 'Pydantic V2 séma, modern szintaxis.'
            
            # Append category and reason
            # Check if already has a category (like [MEGTART])
            if '[MEGTART]' in stripped or '[REFAKTORÁL]' in stripped or '[TÖRÖLHETŐ]' in stripped:
                # Already categorized, keep as is
                new_lines.append(stripped)
            else:
                new_lines.append(f'{stripped} [MEGTART]: {reason}')
            continue
        
        # Non-checklist line
        new_lines.append(stripped)
    
    # Write output
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(new_lines))
    
    # Replace original with updated file
    import shutil
    shutil.move(output_file, input_file)
    print(f"Updated {input_file}")

if __name__ == '__main__':
    main()