#!/usr/bin/env python
"""
Create a simple test page to see if basic Django works
"""

import os
import sys
from pathlib import Path

# Setup
from dotenv import load_dotenv
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / '.env', override=True)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')

import django
django.setup()

from django.conf import settings

print("=" * 60)
print("Creating Test View")
print("=" * 60)
print()

# Create a simple test view file
test_view = """
from django.http import HttpResponse
import sys

def test_view(request):
    info = []
    info.append("<h1>Django Test Page</h1>")
    info.append(f"<p>Python version: {sys.version}</p>")
    info.append(f"<p>Django version: {django.VERSION}</p>")
    
    # Test imports
    try:
        from accounting.models import Account
        count = Account.objects.count()
        info.append(f"<p style='color:green'>✓ Accounting models work: {count} accounts</p>")
    except Exception as e:
        info.append(f"<p style='color:red'>✗ Accounting models error: {e}</p>")
    
    # Test session
    try:
        session_test = request.session.get('test', 'no session')
        info.append(f"<p style='color:green'>✓ Session works: {session_test}</p>")
    except Exception as e:
        info.append(f"<p style='color:red'>✗ Session error: {e}</p>")
    
    # Test context processor
    try:
        from utils.context_processors import branches
        ctx = branches(request)
        info.append(f"<p style='color:green'>✓ Context processor works</p>")
    except Exception as e:
        info.append(f"<p style='color:red'>✗ Context processor error: {e}</p>")
    
    return HttpResponse('<br>'.join(info))
"""

# Write test view
test_file = BASE_DIR / 'accounting' / 'test_view.py'
with open(test_file, 'w') as f:
    f.write(test_view)

print(f"✓ Created: {test_file}")
print()

# Add URL pattern
print("Now add this to accounting/urls.py:")
print()
print("from accounting.test_view import test_view")
print()
print("urlpatterns = [")
print("    path('test/', test_view, name='accounting_test'),")
print("    # ... other patterns")
print("]")
print()
print("=" * 60)
print()
print("Then visit: https://grazuri.uzuriapps.xyz/accounting/test/")
print()
print("This will show exactly what's working and what's not.")
print()
