#!/usr/bin/env python
"""
Check which notifications table exists in production and what columns it has.
"""

import os
import sys
import django
import logging
from django.db import connection

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def check_table_exists(table_name):
    """Check if a table exists"""
    with connection.cursor() as cursor:
        cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
        return cursor.fetchone() is not None

def show_table_structure(table_name):
    """Show the structure of a table"""
    logger.info(f"Structure of {table_name} table:")
    
    with connection.cursor() as cursor:
        cursor.execute(f"DESCRIBE `{table_name}`")
        columns = cursor.fetchall()
        
        for column in columns:
            logger.info(f"  - {column[0]} ({column[1]})")
        
        return [col[0] for col in columns]

def main():
    """Main execution function"""
    logger.info("Checking notifications tables in database...")
    
    # Check different possible notification table names
    possible_tables = [
        'notifications',
        'utils_notification', 
        'reports_notification',
        'notification'
    ]
    
    existing_tables = []
    
    for table_name in possible_tables:
        if check_table_exists(table_name):
            logger.info(f"FOUND: {table_name} table exists")
            existing_tables.append(table_name)
            show_table_structure(table_name)
            logger.info("")
        else:
            logger.info(f"NOT FOUND: {table_name} table does not exist")
    
    if not existing_tables:
        logger.error("No notification tables found!")
    else:
        logger.info(f"Existing notification tables: {existing_tables}")
    
    return True

if __name__ == "__main__":
    main()
