#!/usr/bin/env python
"""
Check utils models and their table names
"""
import os
import django

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

from django.apps import apps
from django.db import connection

print("Checking utils app models...")
print("=" * 60)

# Get utils app models
utils_app = apps.get_app_config('utils')
models = list(utils_app.get_models())

print(f"\nFound {len(models)} models in utils app:")
for model in models:
    table_name = model._meta.db_table
    print(f"\n  Model: {model.__name__}")
    print(f"  Table: {table_name}")
    
    # Check if table exists
    with connection.cursor() as cursor:
        cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
        exists = cursor.fetchone()
        if exists:
            print(f"  Status: ✓ EXISTS")
        else:
            print(f"  Status: ✗ MISSING")

print("\n" + "=" * 60)

# Now create missing tables
print("\nCreating missing tables...")

with connection.schema_editor() as schema_editor:
    for model in models:
        table_name = model._meta.db_table
        
        with connection.cursor() as cursor:
            cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
            exists = cursor.fetchone()
            
            if not exists:
                try:
                    schema_editor.create_model(model)
                    print(f"  ✓ Created {table_name}")
                except Exception as e:
                    print(f"  ⚠ Error creating {table_name}: {e}")

print("\n" + "=" * 60)
print("Done!")
