<?php
/**
 * database_actions.php — the MySQL Database Management POST actions, extracted
 * so they can be driven both by the page (non-JS full-reload fallback) and by
 * api/database-ajax.php (async JSON). Logic is unchanged from the original page
 * handler; it reads $_POST + AUTH_USER and returns ['message'=>..., 'type'=>...].
 */
function whp_database_handle_action(mysqlmgmt $MySQLMgmt): array {
    $message = '';
    $message_type = '';

    if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' || !isset($_POST['action'])) {
        return ['message' => $message, 'type' => $message_type];
    }

    switch ($_POST['action']) {
        case 'create_database':
            if (!empty($_POST['database_name'])) {
                $db_name = $_POST['database_name'];
                if (AUTH_USER !== 'root' && strpos($db_name, AUTH_USER . '_') !== 0) {
                    $db_name = AUTH_USER . '_' . $db_name;
                }
                if (!preg_match('/^[a-zA-Z0-9_]+$/', $db_name)) {
                    $message = 'Database name can only contain letters, numbers, and underscores';
                    $message_type = 'danger';
                } else {
                    $result = $MySQLMgmt->createDatabase($db_name, AUTH_USER);
                    if ($result['status'] === '0') {
                        $message = 'Database created successfully: ' . $db_name;
                        $message_type = 'success';
                    } else {
                        $message = 'Error creating database: ' . $result['msg'];
                        $message_type = 'danger';
                    }
                }
            } else {
                $message = 'Database name is required';
                $message_type = 'danger';
            }
            break;
        case 'delete_database':
            if (!empty($_POST['database_name'])) {
                $database_name = $_POST['database_name'];
                if (!$MySQLMgmt->canManageDatabase(AUTH_USER, $database_name)) {
                    $message = 'You do not have permission to delete this database';
                    $message_type = 'danger';
                } else {
                    $result = $MySQLMgmt->deleteDatabase($database_name);
                    if ($result['status'] === '0') {
                        $message = 'Database deleted successfully: ' . $database_name;
                        $message_type = 'success';
                    } else {
                        $message = 'Error deleting database: ' . $result['msg'];
                        $message_type = 'danger';
                    }
                }
            } else {
                $message = 'Database name is required';
                $message_type = 'danger';
            }
            break;
        case 'create_database_user':
            if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['database_name'])) {
                $db_user = $_POST['username'];
                $db_name = $_POST['database_name'];
                if (AUTH_USER !== 'root') {
                    if (strpos($db_user, AUTH_USER . '_') !== 0) {
                        $db_user = AUTH_USER . '_' . $db_user;
                    }
                    if (strpos($db_name, AUTH_USER . '_') !== 0) {
                        $db_name = AUTH_USER . '_' . $db_name;
                    }
                }
                if (!preg_match('/^[a-zA-Z0-9_]+$/', $db_user)) {
                    $message = 'Username can only contain letters, numbers, and underscores';
                    $message_type = 'danger';
                } else {
                    $privileges = isset($_POST['privileges']) ? $_POST['privileges'] : ['ALL'];
                    $result = $MySQLMgmt->createDatabaseUser($db_user, $_POST['password'], $db_name, $privileges);
                    if ($result['status'] === '0') {
                        $message = 'Database user created successfully: ' . $db_user;
                        $message_type = 'success';
                    } else {
                        $message = 'Error creating database user: ' . $result['msg'];
                        $message_type = 'danger';
                    }
                }
            } else {
                $message = 'Username, password, and database name are required';
                $message_type = 'danger';
            }
            break;
        case 'delete_database_user':
            if (!empty($_POST['username'])) {
                $target_user = $_POST['username'];
                if (!$MySQLMgmt->canManageDatabaseUser(AUTH_USER, $target_user)) {
                    $message = 'You do not have permission to delete this database user';
                    $message_type = 'danger';
                } else {
                    $result = $MySQLMgmt->deleteDatabaseUser($target_user);
                    if ($result['status'] === '0') {
                        $message = 'Database user deleted successfully: ' . $target_user;
                        $message_type = 'success';
                    } else {
                        $message = 'Error deleting database user: ' . $result['msg'];
                        $message_type = 'danger';
                    }
                }
            } else {
                $message = 'Username is required';
                $message_type = 'danger';
            }
            break;
        case 'update_privileges':
            if (!empty($_POST['username']) && !empty($_POST['database_name']) && isset($_POST['privileges'])) {
                $result = $MySQLMgmt->updateDatabaseUserPrivileges($_POST['username'], $_POST['database_name'], $_POST['privileges']);
                if ($result['status'] === '0') {
                    $message = 'Privileges updated successfully for user: ' . $_POST['username'];
                    $message_type = 'success';
                } else {
                    $message = 'Error updating privileges: ' . $result['msg'];
                    $message_type = 'danger';
                }
            } else {
                $message = 'Username, database name, and privileges are required';
                $message_type = 'danger';
            }
            break;
    }

    return ['message' => $message, 'type' => $message_type];
}
