/**
 * Core functions for the plugin
 *
 * @link       https://mementor.no
 * @since      1.0.0
 *
 * @package    TTSWP
 * @subpackage Mementor_TTS/admin/includes
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Initialize core functionality
 */
function mementor_tts_init_core() {
    // Add initialization action
    do_action('mementor_tts_core_init');
    
    // Note: WordPress.org automatically handles translations for plugins since WP 4.6+
}

/**
 * Initialize admin functionality
 */
function mementor_tts_init_admin() {
    // Add admin initialization action
    do_action('mementor_tts_admin_init');
}

/**
 * Register core hooks
 */
function mementor_tts_register_core_hooks() {
    add_action('init', 'mementor_tts_init_core');
    add_action('admin_init', 'mementor_tts_init_admin');
}
add_action('plugins_loaded', 'mementor_tts_register_core_hooks');

/**
 * Get plugin settings with defaults
 */
function mementor_tts_get_settings() {
    $defaults = array(
        'api_key' => '',
        'service' => 'elevenlabs',
        'voice' => '',
        'language' => 'en',
        'model' => 'eleven_flash_v2_5',
        'quality' => '128',
        'debug_mode' => '0'
    );
    
    return get_option('mementor_tts_settings', $defaults);
}

/**
 * Get plugin version
 */
function mementor_tts_get_version() {
    return get_option('mementor_tts_version', '1.0.0');
}

/**
 * Check if debug mode is enabled
 */
function mementor_tts_is_debug_mode() {
    return get_option('mementor_tts_debug_mode', '0') === '1';
}

/**
 * Get available voices
 */
function mementor_tts_get_voices() {
    $api = Mementor_TTS_ElevenLabs_API::get_instance();
    return $api->get_voices();
}

/**
 * Get available models
 */
function mementor_tts_get_models() {
    $api = Mementor_TTS_ElevenLabs_API::get_instance();
    return $api->get_models();
}

/**
 * Get user info
 */
function mementor_tts_get_user_info() {
    $api = Mementor_TTS_ElevenLabs_API::get_instance();
    return $api->get_user_info();
}

/**
 * Clear cache
 */
function mementor_tts_clear_cache() {
    $api = Mementor_TTS_ElevenLabs_API::get_instance();
    $api->clear_cache();
}

/**
 * Get upload directory information
 */
function mementor_tts_get_upload_dir() {
    if (!function_exists('wp_upload_dir')) {
        return false;
    }
    
    $upload_dir = wp_upload_dir();
    return array(
        'path' => $upload_dir['basedir'] . '/text-to-speech-tts',
        'url' => $upload_dir['baseurl'] . '/text-to-speech-tts'
    );
}

/**
 * Create required directories
 */
function mementor_tts_create_directories() {
    $upload_info = mementor_tts_get_upload_dir();
    if ($upload_info && !file_exists($upload_info['path'])) {
        wp_mkdir_p($upload_info['path']);
    }
}

/**
 * Check if current user can manage plugin
 */
function mementor_tts_current_user_can_manage() {
    return current_user_can('manage_options');
}

/**
 * Get admin URL for plugin pages
 */
function mementor_tts_get_admin_url($page = '') {
    return admin_url('admin.php?page=text-to-speech-tts' . ($page ? '-' . $page : ''));
}

/**
 * Add nonce field
 */
function mementor_tts_nonce_field($action = -1) {
    return wp_nonce_field($action, '_mementor_tts_nonce', true, false);
}

/**
 * Verify nonce
 */
function mementor_tts_verify_nonce($nonce, $action = -1) {
    return wp_verify_nonce($nonce, $action);
}

/**
 * Add admin notice
 */
function mementor_tts_add_admin_notice($message, $type = 'info') {
    add_action('admin_notices', function() use ($message, $type) {
        printf(
            '<div class="notice notice-%s is-dismissible"><p>%s</p></div>',
            esc_attr($type),
            wp_kses_post($message)
        );
    });
}

/**
 * Log debug message
 */
function mementor_tts_log_message($message, $level = 'info') {
    // Only log if debug mode is enabled or this is an error-level message
    if (!mementor_tts_is_debug_mode() && $level !== 'error') {
        return;
    }

    // Only log if WP_DEBUG_LOG is enabled
    if (!defined('WP_DEBUG_LOG') || !WP_DEBUG_LOG) {
        return;
    }

    $prefix = '[Mementor TTS]';
    if ($level === 'error') {
        $prefix .= ' Error';
    } elseif ($level === 'warning') {
        $prefix .= ' Warning';
    } elseif ($level === 'debug') {
        $prefix .= ' Debug';
    }

    // Use WordPress debug logging if available
    if (function_exists('wp_debug_log')) {
        wp_debug_log($prefix . ': ' . $message);
    } else {
        error_log($prefix . ': ' . $message);
    }
}

/**
 * Check if in admin area
 */
function mementor_tts_is_admin() {
    return is_admin();
}

/**
 * Get available post types
 */
function mementor_tts_get_post_types() {
    return get_post_types(array('public' => true), 'objects');
}

/**
 * Display common error messages in a standardized format
 */
function mementor_tts_display_error($message = '') {
    if (empty($message)) {
        $message = __('An unknown error occurred.', 'text-to-speech-tts');
    }
    echo '<div class="notice notice-error is-dismissible">';
    echo '<p><strong>' . esc_html($message) . '</strong></p>';
    echo '</div>';
}

/**
 * Display success messages in a standardized format
 */
function mementor_tts_display_success($message = '') {
    if (empty($message)) {
        $message = __('Operation completed successfully.', 'text-to-speech-tts');
    }
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p><strong>' . esc_html($message) . '</strong></p>';
    echo '</div>';
}

/**
 * Display warning messages in a standardized format
 */
function mementor_tts_display_warning($message = '') {
    if (empty($message)) {
        $message = __('Warning: Please review the following information.', 'text-to-speech-tts');
    }
    echo '<div class="notice notice-warning is-dismissible">';
    echo '<p><strong>' . esc_html($message) . '</strong></p>';
    echo '</div>';
}

/**
 * Display info messages in a standardized format
 */
function mementor_tts_display_info($message = '') {
    if (empty($message)) {
        $message = __('Information: Please review the following details.', 'text-to-speech-tts');
    }
    echo '<div class="notice notice-info is-dismissible">';
    echo '<p><strong>' . esc_html($message) . '</strong></p>';
    echo '</div>';
}

// Add other core functions here, properly using WordPress hooks 