<!-- Right Sidebar -->
<div class="atfpp-dashboard-sidebar">
    <div class="atfpp-dashboard-status">
        <h3><?php echo esc_html__('Auto Translation Status', $text_domain); ?></h3>
        <div class="atfpp-dashboard-sts-top">
            <?php
            $service_providers = array();
            $all_data = get_option('cpt_dashboard_data', array());
            $avilable_service_providers = array('google'=>'Google', 'yandex'=>'Yandex', 'localAiTranslator'=>'Chrome AI Translator', 'google_ai'=>'Gemini', 'openai_ai'=>'OpenAI', 'deepl_ai'=>'DeepL');

            if (!is_array($all_data) || !isset($all_data['atfp'])) {

                $all_data['atfp'] = []; // Ensure $all_data['atfp'] is an array

            }

            $totals = array_reduce($all_data['atfp'] ?? [], function($carry, $translation) use (&$service_providers, $avilable_service_providers) {
                // Ensure $translation['string_count'] is numeric
                // Ensure all values are properly handled
                $carry['string_count'] += intval($translation['string_count'] ?? 0);
                $carry['character_count'] += intval($translation['character_count'] ?? 0);
                $carry['time_taken'] += intval($translation['time_taken'] ?? 0);
            
                if(isset($translation['service_provider']) && !empty($translation['service_provider']) && !in_array($translation['service_provider'], $service_providers) && in_array($translation['service_provider'], array_keys($avilable_service_providers))){
                    $service_providers[] = $translation['service_provider'];
                }
                // Count total translations instead of unique post IDs
                if (!empty($translation['post_id'])) {
                    $carry['translation_count']++;
                }
                return $carry;
            }, ['string_count' => 0, 'character_count' => 0, 'time_taken' => 0, 'translation_count' => 0]);

            // Update the time taken string using the new function
            $time_taken_str = atfp_format_time_taken($totals['time_taken'] ,$text_domain);
            ?>
            <span><?php echo esc_html(atfp_format_number($totals['character_count'], $text_domain)); ?></span>
            <span><?php echo esc_html__('Total Characters Translated!', $text_domain); ?></span>
        </div>
        <ul class="atfpp-dashboard-sts-btm">
            <li><span><?php echo esc_html__('Total Strings', $text_domain); ?></span> <span><?php echo esc_html(atfp_format_number($totals['string_count'], $text_domain)); ?></span></li>
            <li><span><?php echo esc_html__('Total Pages / Posts', $text_domain); ?></span> <span><?php echo esc_html($totals['translation_count']); ?></span></li>
            <li><span><?php echo esc_html__('Time Taken', $text_domain); ?></span> <span><?php echo esc_html($time_taken_str); ?></span></li>
            <?php if(count($service_providers) > 0): ?>
                <li class="atfpp-dashboard-sts-btm-service-providers"><span><?php echo esc_html__('Service Providers', $text_domain); ?></span> <div class="atfpp-dashboard-sts-btm-service-providers-list"><?php foreach($service_providers as $service_provider): ?>
                        <span><?php echo esc_html($avilable_service_providers[$service_provider]); ?></span>
                    <?php endforeach; ?></div></li>
            <?php endif; ?>
        </ul>
    </div>
    <div class="atfpp-dashboard-translate-full">
        <h3><?php echo esc_html__('Automatically Translate Plugins & Themes', $text_domain); ?></h3>
        <div class="atfpp-dashboard-addon first">
            <div class="atfpp-dashboard-addon-l">
                <strong><?php echo esc_html(atfp_get_plugin_display_name('automatic-translator-addon-for-loco-translate', $text_domain)); ?></strong>
                <span class="addon-desc"><?php echo esc_html__('Loco addon to translate plugins and themes.', $text_domain); ?></span>
                <?php if (atfp_is_plugin_installed('automatic-translator-addon-for-loco-translate')): ?>
                    <span class="installed"><?php echo esc_html__('Installed', $text_domain); ?></span>
                <?php else: ?>
                    <a href="<?php echo esc_url(admin_url('plugin-install.php?s=LocoAI+–+Auto+Translate+for+Loco+Translate+by+CoolPlugins&tab=search&type=term') ); ?>" class="atfpp-dashboard-btn" target="_blank"><?php _e('Install', $text_domain); ?></a>
                <?php endif; ?>
            </div>
            <div class="atfpp-dashboard-addon-r">
                <img src="<?php echo esc_url(ATFPP_URL . 'admin/atfpp-dashboard/images/atlt-logo.png'); ?>" alt="<?php echo esc_html__('TranslatePress Addon', $text_domain); ?>">
            </div>
        </div>
    </div>
    <div class="atfpp-dashboard-rate-us">
        <h3><?php echo esc_html__('Rate Us ⭐⭐⭐⭐⭐', $text_domain); ?></h3>
        <p><?php echo esc_html__('We\'d love your feedback! Hope this addon made auto-translations easier for you.', $text_domain); ?></p>
        <a href="https://wordpress.org/support/plugin/automatic-translations-for-polylang/reviews/#new-post" class="review-link" target="_blank"><?php echo esc_html__('Submit a Review →', $text_domain); ?></a>
    </div>
</div>

<?php

function atfp_format_time_taken($time_taken, $text_domain) {
    if ($time_taken === 0) return esc_html__('0', $text_domain);
    if ($time_taken < 60) return sprintf(esc_html__('%d sec', $text_domain), $time_taken);
    if ($time_taken < 3600) {
        $min = floor($time_taken / 60);
        $sec = $time_taken % 60;
        return sprintf(esc_html__('%d min %d sec', $text_domain), $min, $sec);
    }
    $hours = floor($time_taken / 3600);
    $min = floor(($time_taken % 3600) / 60);
    return sprintf(esc_html__('%d hours %d min', $text_domain), $hours, $min);
}

function atfp_is_plugin_installed($plugin_slug) {
    $plugins = get_plugins();
    
    // Check if the plugin is installed
    if ($plugin_slug === 'automatic-translator-addon-for-loco-translate') {
        return isset($plugins['automatic-translator-addon-for-loco-translate/automatic-translator-addon-for-loco-translate.php']) || isset($plugins['loco-automatic-translate-addon-pro/loco-automatic-translate-addon-pro.php']);
    }
    return false; // Return false if no match found
}

function atfp_get_plugin_display_name($plugin_slug, $text_domain) {
    $plugins = get_plugins();

    // Define free and pro plugin paths
    $plugin_paths = [
        'automatic-translator-addon-for-loco-translate' => [
            'free' => 'automatic-translator-addon-for-loco-translate/automatic-translator-addon-for-loco-translate.php',
            'pro'  => 'loco-automatic-translate-addon-pro/loco-automatic-translate-addon-pro.php',
            'free_name' => esc_html__('LocoAI – Auto Translate For Loco Translate', $text_domain),
            'pro_name'  => esc_html__('LocoAI – Auto Translate for Loco Translate (Pro)', $text_domain),
        ],
    ];

    // Check if the provided plugin slug exists
    if (!isset($plugin_paths[$plugin_slug])) {
        return $plugin_slug['free_name'];
    }

    $free_installed = isset($plugins[$plugin_paths[$plugin_slug]['free']]);
    $pro_installed = isset($plugins[$plugin_paths[$plugin_slug]['pro']]);

    // Determine which version is installed
    if ($pro_installed) {
        return $plugin_paths[$plugin_slug]['pro_name'];
    } elseif ($free_installed) {
        return $plugin_paths[$plugin_slug]['free_name'];
    } else {
        return $plugin_paths[$plugin_slug]['free_name'];
    }
}

function atfp_format_number($number, $text_domain) {
    if ($number >= 1000000000) {
        return round($number / 1000000000, 1) . esc_html__('B', $text_domain);
    } elseif ($number >= 1000000) {
        return round($number / 1000000, 1) . esc_html__('M', $text_domain);
    } elseif ($number >= 1000) {
        return round($number / 1000, 1) . esc_html__('K', $text_domain);
    }
    return $number;
}

