#!/usr/bin/env python
"""
Quick patch to fix settings.py on production server
This adds override=True to the load_dotenv call
"""

import os
from pathlib import Path

print("=" * 60)
print("Patching settings.py to fix .env loading")
print("=" * 60)
print()

# Find settings.py
settings_path = Path(__file__).resolve().parent / 'branch_system' / 'settings.py'

if not settings_path.exists():
    print(f"ERROR: settings.py not found at {settings_path}")
    exit(1)

print(f"Found settings.py at: {settings_path}")

# Read current content
with open(settings_path, 'r') as f:
    content = f.read()

# Check if already patched
if 'override=True' in content:
    print("✓ Already patched! No changes needed.")
    exit(0)

# Apply patch
old_line = "load_dotenv(BASE_DIR / '.env')"
new_line = "load_dotenv(BASE_DIR / '.env', override=True)"

if old_line in content:
    content = content.replace(old_line, new_line)
    
    # Write back
    with open(settings_path, 'w') as f:
        f.write(content)
    
    print("✓ Patched successfully!")
    print()
    print(f"Changed: {old_line}")
    print(f"To:      {new_line}")
    print()
    print("Now run: python simple_accounting_deploy.py")
else:
    print("ERROR: Could not find the line to patch")
    print(f"Looking for: {old_line}")
    print()
    print("Please manually edit branch_system/settings.py")
    print("Change: load_dotenv(BASE_DIR / '.env')")
    print("To:     load_dotenv(BASE_DIR / '.env', override=True)")

print()
print("=" * 60)
