#!/usr/bin/env python
"""
Verify production setup is correct
"""

import os
import sys
from pathlib import Path

print("=" * 60)
print("Production Configuration Verification")
print("=" * 60)
print()

BASE_DIR = Path(__file__).resolve().parent

# Check 1: local_settings.py
print("[1/5] Checking local_settings.py...")
local_settings = BASE_DIR / 'branch_system' / 'local_settings.py'
if local_settings.exists():
    print("  ✗ local_settings.py EXISTS - this will override production config!")
    print(f"  Location: {local_settings}")
    print("  Action: Delete or rename this file")
else:
    print("  ✓ local_settings.py does not exist")
print()

# Check 2: .env file
print("[2/5] Checking .env file...")
env_file = BASE_DIR / '.env'
if env_file.exists():
    print("  ✓ .env file exists")
    from dotenv import load_dotenv
    load_dotenv(env_file)
    print(f"  DB_NAME: {os.getenv('DB_NAME', 'NOT SET')}")
    print(f"  DB_USER: {os.getenv('DB_USER', 'NOT SET')}")
else:
    print("  ✗ .env file NOT found")
print()

# Check 3: Settings.py
print("[3/5] Checking settings.py...")
settings_file = BASE_DIR / 'branch_system' / 'settings.py'
if settings_file.exists():
    with open(settings_file, 'r') as f:
        content = f.read()
        if 'override=True' in content:
            print("  ✓ Settings has override=True")
        else:
            print("  ✗ Settings missing override=True")
        
        if "'grazuri'" in content and "# 'grazuri'" not in content:
            print("  ✗ grazuri app is enabled - causes conflicts!")
        else:
            print("  ✓ grazuri app is disabled")
else:
    print("  ✗ settings.py NOT found")
print()

# Check 4: Django setup
print("[4/5] Testing Django database connection...")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
try:
    import django
    django.setup()
    from django.db import connection
    
    with connection.cursor() as cursor:
        cursor.execute("SELECT DATABASE()")
        db_name = cursor.fetchone()[0]
        print(f"  ✓ Connected to database: {db_name}")
        
        cursor.execute("SELECT COUNT(*) FROM accounting_accounts")
        count = cursor.fetchone()[0]
        print(f"  ✓ Accounting accounts: {count}")
except Exception as e:
    print(f"  ✗ Django connection failed: {e}")
print()

# Check 5: Accounting app
print("[5/5] Checking accounting app...")
try:
    from accounting.models import Account, FiscalPeriod
    print(f"  ✓ Account model loaded")
    print(f"  ✓ FiscalPeriod model loaded")
except Exception as e:
    print(f"  ✗ Failed to import models: {e}")
print()

print("=" * 60)
print("Verification Complete")
print("=" * 60)
print()
print("If you see ✗ marks above, fix those issues.")
print("The server error might be due to local_settings.py")
print()
