import os
import sys
import django
from django.conf import settings
from django.core.management import utils

# Setup Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

def update_settings_file():
    print("\n=== Updating Production Settings ===\n")
    
    settings_file = 'branch_system/settings.py'
    settings_production_file = 'branch_system/settings_production.py'
    
    try:
        # First read the current settings
        with open(settings_file, 'r') as f:
            content = f.read()
        
        # Prepare production settings
        production_settings = f"""from .settings import *

# Security Settings
DEBUG = False
ALLOWED_HOSTS = ['branchbusinessadvance.co.ke', 'www.branchbusinessadvance.co.ke']

# Session Settings
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000  # 1 year
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SESSION_COOKIE_AGE = 3600  # 1 hour
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

# Static Files
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

# Media Files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

# Email Settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
"""
        
        # Write production settings
        with open(settings_production_file, 'w') as f:
            f.write(production_settings)
        
        print(f"✓ Created {settings_production_file}")
        print("\nTo use these settings:")
        print("1. In cPanel, set environment variable:")
        print("   DJANGO_SETTINGS_MODULE=branch_system.settings_production")
        print("\n2. Or modify passenger_wsgi.py to include:")
        print('   os.environ["DJANGO_SETTINGS_MODULE"] = "branch_system.settings_production"')
        
        # Also create a check settings script
        check_script = """import os
import django
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings_production')
django.setup()

print('\\nCurrent Django Settings:\\n')
print(f'DEBUG: {settings.DEBUG}')
print(f'ALLOWED_HOSTS: {settings.ALLOWED_HOSTS}')
print(f'SESSION_COOKIE_SECURE: {settings.SESSION_COOKIE_SECURE}')
print(f'STATIC_ROOT: {settings.STATIC_ROOT}')
"""
        
        with open('check_settings.py', 'w') as f:
            f.write(check_script)
            
        print("\n✓ Created check_settings.py to verify settings")
        print("   Run: python3 check_settings.py")
        
    except Exception as e:
        print(f"Error: {str(e)}")
        return False
    
    return True

if __name__ == '__main__':
    try:
        if update_settings_file():
            print("\n=== Settings Update Complete ===")
            print("\nPlease restart your Django application after applying these changes!")
        else:
            print("\nFailed to update settings")
            sys.exit(1)
    except Exception as e:
        print(f"\nError: {str(e)}")
        sys.exit(1) 