<?php
/**
 * app/functions/common.php
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
function getClassName($class) {
    $className = get_class($class);
    $classNameParts = explode('\\', $className);
    return end($classNameParts);
}

function convertClassNameToFileName( $class_name ) {
    $lower = lcfirst( $class_name ) . '.php';
    $upper_split = preg_split( '/(?=[A-Z])/', $lower );
    $file_name = strtolower( implode( '_', $upper_split ) );
    return $file_name;
}

function convertFileNameToClassName( $file_name ) {
    $file_name = str_ireplace( '.php', '', $file_name );
    $file_name = rtrim( $file_name, DIRECTORY_SEPARATOR );
    $class_name = '';
    if ( stripos( $file_name, '_' ) ) {
        $exploded = explode( '_', $file_name );
        foreach ( $exploded as $key => $value ) {
            $class_name .= ucfirst( $value );
        }
    } else {
        $class_name .= ucfirst( $file_name );
    }
    return $class_name;
}

function convertFolderToClassName( $folder ) {
    $file_name = rtrim( $folder, DIRECTORY_SEPARATOR );
    $parts_array = explode(DIRECTORY_SEPARATOR, $file_name);
    $file_name = array_pop($parts_array);

    $class_name = '';
    if ( stripos( $file_name, '_' ) ) {
        $exploded = explode( '_', $file_name );
        foreach ( $exploded as $key => $value ) {
            $class_name .= ucfirst( $value );
        }
    } else {
        $class_name .= ucfirst( $file_name );
    }
    return $class_name;
}

function convertFileNameToPluginClass( $file_name ) {
    $class_name = convertFolderToClassName( $file_name );
    $class = (string) APP_SPACE . '\\Plugins\\' . $class_name;
    return $class;
}

function convertFileNameToModelClass( $file_name ) {
    $class_name = convertFileNameToClassName( $file_name );
    $class = (string) APP_SPACE . '\\Models\\' . $class_name;
    return $class;
}

function getFileList( $folder = '' ) {
    if ( empty( $folder ) ) {
        $folder = PLUGIN_DIRECTORY;
    }
    if ( !file_exists( $folder ) ) {
        return false;
    }
    $pluginFolders = scandir( $folder );
    array_shift( $pluginFolders ); // remove the .
    array_shift( $pluginFolders ); // remove the ..
    return $pluginFolders;
}

function dv( $variable ) {
    echo '<pre>';
    echo var_export( $variable, true );
    echo '</pre>';
    exit;
}

function iv( $variable ) {
    echo '<pre>';
    echo var_export( $variable, true );
    echo '</pre>';
}

function generateToken(): string {
    return bin2hex(random_bytes(32)); // Generates a 64-character hexadecimal token
}

function generateRandomString( $length = 10 ) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen( $characters );
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[random_int(0, $charactersLength - 1)];
    }
    return $randomString;
}

function generateUuidV4(): string {
    // Generate 16 random bytes
    $data = random_bytes(16);

    // Set the version to 4 -> random (bits 12-15 of time_hi_and_version)
    $data[6] = chr((ord($data[6]) & 0x0f) | 0x40);

    // Set the variant to RFC 4122 -> (bits 6-7 of clock_seq_hi_and_reserved)
    $data[8] = chr((ord($data[8]) & 0x3f) | 0x80);

    // Convert to hexadecimal and format as a UUID
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}