"""
DIALECT_QUERIES - Database-specific SQL for introspection methods.
Used by SchemaDBConnection for cross-database compatibility.
"""

DIALECT_QUERIES = {
    "postgresql": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            WHERE schema_name NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
            ORDER BY schema_name
        """,
    },

    "snowflake": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            ORDER BY schema_name
        """,
    },

    "bigquery": {
        "get_schema_tables": """
            SELECT table_name
            FROM `{project}.{schema}.INFORMATION_SCHEMA.TABLES`
            WHERE table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM `{project}.{schema}.INFORMATION_SCHEMA.COLUMNS`
            WHERE table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": "SELECT * FROM `{project}.{schema}.{table}` LIMIT :limit",
        "get_table_count": "SELECT COUNT(*) AS count FROM `{project}.{schema}.{table}`",
        "get_schemas": """
            SELECT schema_name
            FROM `{project}.INFORMATION_SCHEMA.SCHEMATA`
            ORDER BY schema_name
        """,
    },

    "redshift": {
        "get_schema_tables": """
            SELECT tablename AS table_name
            FROM pg_tables
            WHERE schemaname = :schema
            ORDER BY tablename
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            WHERE schema_name NOT IN ('pg_catalog', 'information_schema', 'pg_internal')
            ORDER BY schema_name
        """,
    },

    "clickhouse": {
        "get_schema_tables": """
            SELECT name AS table_name
            FROM system.tables
            WHERE database = :schema
              AND engine NOT IN ('View', 'MaterializedView')
            ORDER BY name
        """,
        "get_table_columns": """
            SELECT name AS column_name, type AS data_type, 
                   if(type LIKE 'Nullable%', 'YES', 'NO') AS is_nullable,
                   default_expression AS column_default
            FROM system.columns
            WHERE database = :schema AND table = :table
            ORDER BY position
        """,
        "get_table_sample": "SELECT * FROM {schema}.{table} LIMIT :limit",
        "get_table_count": "SELECT COUNT(*) AS count FROM {schema}.{table}",
        "get_schemas": """
            SELECT name AS schema_name
            FROM system.databases
            WHERE name NOT IN ('system', 'INFORMATION_SCHEMA', 'information_schema')
            ORDER BY name
        """,
    },

    "duckdb": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            ORDER BY schema_name
        """,
    },

    "mysql": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": "SELECT * FROM `{schema}`.`{table}` LIMIT :limit",
        "get_table_count": "SELECT COUNT(*) AS count FROM `{schema}`.`{table}`",
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            WHERE schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
            ORDER BY schema_name
        """,
    },

    "sqlserver": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": "SELECT TOP (:limit) * FROM [{schema}].[{table}]",
        "get_table_count": "SELECT COUNT(*) AS count FROM [{schema}].[{table}]",
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            WHERE schema_name NOT IN ('sys', 'guest', 'INFORMATION_SCHEMA')
            ORDER BY schema_name
        """,
    },

    "databricks": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": "SELECT * FROM `{schema}`.`{table}` LIMIT :limit",
        "get_table_count": "SELECT COUNT(*) AS count FROM `{schema}`.`{table}`",
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            ORDER BY schema_name
        """,
    },

    "athena": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, '' AS column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            ORDER BY schema_name
        """,
    },

    "trino": {
        "get_schema_tables": """
            SELECT table_name
            FROM information_schema.tables
            WHERE table_schema = :schema
              AND table_type = 'BASE TABLE'
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, '' AS column_default
            FROM information_schema.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM information_schema.schemata
            ORDER BY schema_name
        """,
    },

    "sparksql": {
        "get_schema_tables": """
            SHOW TABLES IN {schema}
        """,
        "get_table_columns": """
            DESCRIBE {schema}.{table}
        """,
        "get_table_sample": "SELECT * FROM {schema}.{table} LIMIT :limit",
        "get_table_count": "SELECT COUNT(*) AS count FROM {schema}.{table}",
        "get_schemas": """
            SHOW DATABASES
        """,
    },

    "vertica": {
        "get_schema_tables": """
            SELECT table_name
            FROM v_catalog.tables
            WHERE table_schema = :schema
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, is_nullable, column_default
            FROM v_catalog.columns
            WHERE table_schema = :schema AND table_name = :table
            ORDER BY ordinal_position
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" LIMIT :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schema_name
            FROM v_catalog.schemata
            WHERE NOT is_system_schema
            ORDER BY schema_name
        """,
    },

    "oracle": {
        "get_schema_tables": """
            SELECT table_name
            FROM all_tables
            WHERE owner = UPPER(:schema)
            ORDER BY table_name
        """,
        "get_table_columns": """
            SELECT column_name, data_type, nullable AS is_nullable, data_default AS column_default
            FROM all_tab_columns
            WHERE owner = UPPER(:schema) AND table_name = UPPER(:table)
            ORDER BY column_id
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" WHERE ROWNUM <= :limit',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT username AS schema_name
            FROM all_users
            ORDER BY username
        """,
    },

    "teradata": {
        "get_schema_tables": """
            SELECT TableName AS table_name
            FROM DBC.TablesV
            WHERE DatabaseName = :schema
              AND TableKind = 'T'
            ORDER BY TableName
        """,
        "get_table_columns": """
            SELECT ColumnName AS column_name, ColumnType AS data_type,
                   CASE WHEN Nullable = 'Y' THEN 'YES' ELSE 'NO' END AS is_nullable,
                   DefaultValue AS column_default
            FROM DBC.ColumnsV
            WHERE DatabaseName = :schema AND TableName = :table
            ORDER BY ColumnId
        """,
        "get_table_sample": 'SELECT TOP :limit * FROM "{schema}"."{table}"',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT DatabaseName AS schema_name
            FROM DBC.DatabasesV
            ORDER BY DatabaseName
        """,
    },

    "db2": {
        "get_schema_tables": """
            SELECT tabname AS table_name
            FROM syscat.tables
            WHERE tabschema = UPPER(:schema)
              AND type = 'T'
            ORDER BY tabname
        """,
        "get_table_columns": """
            SELECT colname AS column_name, typename AS data_type,
                   CASE WHEN nulls = 'Y' THEN 'YES' ELSE 'NO' END AS is_nullable,
                   default AS column_default
            FROM syscat.columns
            WHERE tabschema = UPPER(:schema) AND tabname = UPPER(:table)
            ORDER BY colno
        """,
        "get_table_sample": 'SELECT * FROM "{schema}"."{table}" FETCH FIRST :limit ROWS ONLY',
        "get_table_count": 'SELECT COUNT(*) AS count FROM "{schema}"."{table}"',
        "get_schemas": """
            SELECT schemaname AS schema_name
            FROM syscat.schemata
            WHERE schemaname NOT LIKE 'SYS%'
            ORDER BY schemaname
        """,
    },
}
