# /opt/docker/dev/service_finder/backend/app/scripts/sync_python_models_generator.py
# 
import asyncio
from sqlalchemy import inspect
from sqlalchemy.ext.asyncio import create_async_engine
from app.core.config import settings
import sqlalchemy.types as types
# PostgreSQL specifikus típusok importálása
from sqlalchemy.dialects.postgresql import JSONB, UUID, ENUM

# Típus leképezés javítva
TYPE_MAP = {
    types.INTEGER: "Integer",
    types.VARCHAR: "String",
    types.TEXT: "String",
    types.BOOLEAN: "Boolean",
    types.DATETIME: "DateTime",
    types.TIMESTAMP: "DateTime",
    types.NUMERIC: "Numeric",
    types.JSON: "JSON",
    JSONB: "JSONB",
    UUID: "UUID"
}

async def generate_perfect_models():
    engine = create_async_engine(str(settings.SQLALCHEMY_DATABASE_URI))
    
    def analyze(connection):
        inspector = inspect(connection)
        # Csak azokat a sémákat nézzük, ahol extra adatot találtunk
        schemas = ['gamification', 'identity', 'marketplace', 'system', 'vehicle']
        
        print("\n" + "="*80)
        print(f"{'🛠️  PONTOS PYTHON MODELL KÓDOK A HIÁNYZÓ ELEMEKHEZ':^80}")
        print("="*80)

        for schema in schemas:
            tables = inspector.get_table_names(schema=schema)
            for table_name in tables:
                # Osztálynév generálás (pl. user_contributions -> UserContribution)
                class_name = "".join(x.capitalize() for x in table_name.split("_"))
                if class_name.endswith("s"): class_name = class_name[:-1]

                print(f"\n# --- [{schema}.{table_name}] ---")
                
                for col in inspector.get_columns(table_name, schema=schema):
                    # Típus meghatározása intelligensebben
                    col_raw_type = col['type']
                    col_type = "String"
                    for k, v in TYPE_MAP.items():
                        if isinstance(col_raw_type, k):
                            col_type = v
                            break
                    
                    params = []
                    if col.get('primary_key'): params.append("primary_key=True")
                    if not col.get('nullable'): params.append("nullable=False")
                    
                    param_str = ", ".join(params)
                    print(f"{col['name']} = Column({col_type}{', ' + param_str if param_str else ''})")

    async with engine.begin() as conn:
        await conn.run_sync(analyze)
    await engine.dispose()

if __name__ == "__main__":
    asyncio.run(generate_perfect_models())