#!/usr/bin/env python3
"""
Script to replace all remaining legacy Verpachtung references with LandVerpachtung
"""

import re

def replace_verpachtung_references():
    with open('app/stiftung/views.py', 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Replace Verpachtung.objects with LandVerpachtung.objects
    content = re.sub(r'\bVerpachtung\.objects\b', 'LandVerpachtung.objects', content)
    
    # Replace legacy field names with new ones
    content = re.sub(r'pachtzins_jaehrlich', 'pachtzins_pauschal', content)
    
    # Replace legacy field references in aggregates
    content = re.sub(r"Sum\('verpachtung__pachtzins_jaehrlich'\)", "Sum('landverpachtung__pachtzins_pauschal')", content)
    content = re.sub(r"Sum\('verpachtung__verpachtete_flaeche'\)", "Sum('landverpachtung__verpachtete_flaeche')", content)
    
    # Update related name references
    content = re.sub(r"verpachtung_set", "landverpachtung_set", content)
    content = re.sub(r"verpachtung_id", "land_verpachtung_id", content)
    
    with open('app/stiftung/views.py', 'w', encoding='utf-8') as f:
        f.write(content)
    
    print("Updated all legacy Verpachtung references to use LandVerpachtung")

if __name__ == "__main__":
    replace_verpachtung_references()
