"""Force clear all Django caches on production"""
import os
import sys
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings_production')
os.environ['DB_NAME'] = 'xygbfpsg_loans'
os.environ['DB_USER'] = 'xygbfpsg_graz'
os.environ['DB_PASSWORD'] = 'j.ez-xy6##y.rllB'
os.environ['DB_HOST'] = 'localhost'

django.setup()

from django.core.cache import cache
from django.template.loader import get_template
import shutil

print("=" * 60)
print("CLEARING ALL DJANGO CACHES")
print("=" * 60)

# Clear Django cache
try:
    cache.clear()
    print("✓ Django cache cleared")
except Exception as e:
    print(f"✗ Cache clear error: {e}")

# Clear template cache
try:
    from django.template.loader import render_to_string
    from django.template import engines
    for engine in engines.all():
        if hasattr(engine, 'engine'):
            engine.engine.template_loaders = None
    print("✓ Template cache cleared")
except Exception as e:
    print(f"✗ Template cache error: {e}")

# Remove Python bytecode cache
try:
    pycache_count = 0
    for root, dirs, files in os.walk('.'):
        if '__pycache__' in dirs:
            pycache_dir = os.path.join(root, '__pycache__')
            try:
                shutil.rmtree(pycache_dir)
                pycache_count += 1
            except:
                pass
    print(f"✓ Removed {pycache_count} __pycache__ directories")
except Exception as e:
    print(f"✗ Pycache removal error: {e}")

print()
print("Upload this script to production and run: python3 clear_all_caches.py")
print("Then touch tmp/restart.txt")
print("=" * 60)
