import os
import sys
import django

# Set up Django environment
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.db import connection

def check_notification_tables():
    """Check the structure of notification-related tables in the database"""
    print("Checking notification tables structure...\n")
    
    # Check which tables exist
    with connection.cursor() as cursor:
        # Check notifications table
        cursor.execute("""
            SELECT COUNT(*) 
            FROM information_schema.tables 
            WHERE table_name='notifications';
        """)
        notifications_exists = cursor.fetchone()[0] > 0
        
        # Check utils_notification table
        cursor.execute("""
            SELECT COUNT(*) 
            FROM information_schema.tables 
            WHERE table_name='utils_notification';
        """)
        utils_notification_exists = cursor.fetchone()[0] > 0
    
    print(f"Table 'notifications' exists: {notifications_exists}")
    print(f"Table 'utils_notification' exists: {utils_notification_exists}\n")
    
    # Check columns in notifications table
    if notifications_exists:
        print("Columns in 'notifications' table:")
        with connection.cursor() as cursor:
            cursor.execute("""
                SELECT column_name, data_type, is_nullable
                FROM information_schema.columns
                WHERE table_name='notifications'
                ORDER BY ordinal_position;
            """)
            columns = cursor.fetchall()
            
            for column in columns:
                print(f"  - {column[0]}: {column[1]} (Nullable: {column[2]})")
    
    # Check columns in utils_notification table
    if utils_notification_exists:
        print("\nColumns in 'utils_notification' table:")
        with connection.cursor() as cursor:
            cursor.execute("""
                SELECT column_name, data_type, is_nullable
                FROM information_schema.columns
                WHERE table_name='utils_notification'
                ORDER BY ordinal_position;
            """)
            columns = cursor.fetchall()
            
            for column in columns:
                print(f"  - {column[0]}: {column[1]} (Nullable: {column[2]})")
    
    # Check model definitions
    print("\nChecking model definitions...")
    try:
        from reports.models import Notification as ReportsNotification
        print("\nreports.models.Notification fields:")
        for field in ReportsNotification._meta.get_fields():
            print(f"  - {field.name}: {field.__class__.__name__}")
    except ImportError:
        print("Could not import reports.models.Notification")
    
    try:
        from utils.models import Notification as UtilsNotification
        print("\nutils.models.Notification fields:")
        for field in UtilsNotification._meta.get_fields():
            print(f"  - {field.name}: {field.__class__.__name__}")
    except ImportError:
        print("Could not import utils.models.Notification")
    
    # Check client_delete function
    print("\nChecking users.views.client_delete function...")
    try:
        from users.views import client_delete
        import inspect
        print("\nFunction source:")
        print(inspect.getsource(client_delete))
    except ImportError:
        print("Could not import users.views.client_delete")

if __name__ == "__main__":
    check_notification_tables()