<?php

if (!defined('ABSPATH')) exit; 

if (!function_exists('aieo_get_wp_collation')) {
    
    function aieo_get_wp_collation() {
        global $wpdb;

        
        $collation = $wpdb->get_var(
            "SELECT TABLE_COLLATION FROM INFORMATION_SCHEMA.TABLES
             WHERE TABLE_SCHEMA = DATABASE()
             AND TABLE_NAME = '{$wpdb->posts}'
             LIMIT 1"
        );

        
        if (empty($collation) && !empty($wpdb->collate)) {
            $collation = $wpdb->collate;
        }

        
        if (empty($collation)) {
            $collation = 'utf8mb4_unicode_ci';
        }

        return $collation;
    }
}

if (!function_exists('aieo_get_wp_charset')) {
    
    function aieo_get_wp_charset() {
        global $wpdb;

        
        $charset = $wpdb->get_var(
            "SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLUMNS
             WHERE TABLE_SCHEMA = DATABASE()
             AND TABLE_NAME = '{$wpdb->posts}'
             AND COLUMN_NAME = 'post_title'
             LIMIT 1"
        );

        
        if (empty($charset) && !empty($wpdb->charset)) {
            $charset = $wpdb->charset;
        }

        
        if (empty($charset)) {
            $charset = 'utf8mb4';
        }

        return $charset;
    }
}

if (!function_exists('aieo_get_charset_collate')) {
    
    function aieo_get_charset_collate() {
        $charset = aieo_get_wp_charset();
        $collate = aieo_get_wp_collation();

        $charset_collate = '';

        if (!empty($charset)) {
            $charset_collate = "DEFAULT CHARACTER SET {$charset}";
        }

        if (!empty($collate)) {
            $charset_collate .= " COLLATE {$collate}";
        }

        return $charset_collate;
    }
}

if (!function_exists('aieo_dbdelta')) {
    
    function aieo_dbdelta($sql) {
        if (!function_exists('dbDelta')) {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }
        $clean = array();
        foreach (preg_split('/\r\n|\r|\n/', (string) $sql) as $line) {
            
            
            
            $line = preg_replace('/(^|\s)--(\s.*)?$/', '', $line);
            if (trim($line) === '') {
                continue; 
            }
            $clean[] = rtrim($line);
        }
        return dbDelta(implode("\n", $clean));
    }
}

if (!function_exists('aieo_dmm_table_exists')) {
    
    function aieo_dmm_table_exists($table_name) {
        global $wpdb;

        $found = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
             WHERE TABLE_SCHEMA = DATABASE()
             AND TABLE_NAME = %s",
            $table_name
        ));

        return ((int) $found) > 0;
    }
}

if (!function_exists('aieo_dmm_table_is_empty')) {
    
    function aieo_dmm_table_is_empty($table_name) {
        global $wpdb;

        if (!aieo_dmm_table_exists($table_name)) {
            return true;
        }

        $count = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name}");
        return ((int) $count) === 0;
    }
}

if (!function_exists('aieo_dmm_sanitize_role_column')) {
    
    function aieo_dmm_sanitize_role_column($role) {
        $clean = strtolower(preg_replace('/[^a-z0-9_]/', '_', $role));
        if ($clean === '') {
            return 'role_r';
        }
        if (preg_match('/^[0-9]/', $clean)) {
            $clean = 'r_' . $clean;
        }
        return 'role_' . $clean;
    }
}

require_once dirname(__FILE__) . '/../dmm/sql/dmm-schema.php';
