<?php

/**
 * =============================================================================
 * @package		        Blue Raven (Free Version)
 * Plugin URI		    https://enterrahost.com/plugins/blue-raven
 * 
 * @Author		        EnterraHost
 * @Author URI		    https://enterrahost.com
 * @License		        GPL-2.0-or-later
 *  
 * @Copyright		    (c) 2025 EnterraHost
 * @File Description    Log Reports to Enterrahost
 * 
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN DASHBOARD
 * ||   - EMERGENCY EMAIL ALERT
 * || 
 * =============================================================================
 */

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

/*
============================================================
    BLUE RAVEN ADMIN DASHBOARD
============================================================
*/

class BlueRaven_Admin_Dashboard {
    
    /**
     * Renders the Blue Raven dashboard.
     *
     * This is the main public method called to display the dashboard interface
     * in the WordPress admin. It delegates output to the internal HTML renderer.
     *
     * @access public
     * @static
     * @return void
     */
    public static function render() {
        self::output_dashboard_html();
    }
    
    /**
     * Outputs the main dashboard HTML content.
     *
     * Handles layout and rendering of the dashboard UI, including
     * widgets, filters, or other interface elements specific to Blue Raven.
     *
     * @access private
     * @static
     * @return void
     */
    private static function output_dashboard_html() {
        ?>
        <div class='parent-container'>
            <!--<h2><?php esc_html_e('Home', 'blue-raven'); ?></h2>-->
            <div> 
                <?php if (class_exists('WooCommerce')) { self::sales_analytics_page(); } else { echo '<p class="enterra-highlight" style="padding: 1rem 0rem;">WooCommerce is not active. Please install and activate WooCommerce to use sales analytics.</p>';}?>
            </div>
            <div><?php self::feature_req(); ?> </div>
        </div>
        <?php
    }
    
    /**
     * Outputs the Sales Analytics section.
     *
     * Displays sales performance metrics, trends, and charts for
     * products or services managed through the Blue Raven plugin.
     *
     * @access private
     * @static
     * @return void
     */
    private static function sales_analytics_page() {
        if (!current_user_can('manage_options')) return;
        
        $current_month = gmdate('Y-m'); 
        ?>
        <div style='min-height: 80vh'>
            <div id='sales-analytics' style='display: none;'>
                <h1><?php esc_html_e('Sales Analytics', 'blue-raven'); ?></h1>
    
                <form id="sales-analytics-form" class="sales-analytics-form flex-sb" method="post">
                    <div class='flex-left'>
                        <?php wp_nonce_field('sales_analytics_nonce_action', 'sales_analytics_nonce'); ?>
            
                        <div class="form-group" style=''>
                            <label for="sales-analytics-range"><?php esc_html_e('Date Range:', 'blue-raven'); ?></label>
                            <select id="sales-analytics-range" name="range" class="form-control br-dropdown" style='width: 20vw; max-width: 20rem '>
                                <option value="current_month"><?php esc_html_e('Current Month', 'blue-raven'); ?></option>
                                <option value="last_12_months"><?php esc_html_e('Last 12 Months', 'blue-raven'); ?></option>
                                <option value="year_to_date"><?php esc_html_e('Year to Date', 'blue-raven'); ?></option>
                                <option value="custom_month"><?php esc_html_e('Custom Month', 'blue-raven'); ?></option>
                            </select>
                        </div>
                
                        <div class="form-group" id="month-selector" style="display:none; ">
                            <label for="sales-analytics-month"><?php esc_html_e('Select Month:', 'blue-raven'); ?></label>
                            <select id="sales-analytics-month" name="month" class="form-control br-dropdown" style='width: 20rem;'>
                                <?php
                                $current_month_num = gmdate('n');
                                $current_year = gmdate('Y');
                                for ($i = 1; $i <= 12; $i++) {
                                    $month_num = str_pad($i, 2, '0', STR_PAD_LEFT);
                                    $month_name = gmdate('F', mktime(0, 0, 0, $i, 1));
                                    $value = $current_year . '-' . $month_num;
                                    $selected = ($i == $current_month_num) ? 'selected' : '';
                                    echo '<option value="' . esc_attr($value) . '" ' . selected($i, $current_month_num, false) . '>' . esc_html($month_name) . '</option>';
                                }
                                ?>
                            </select>
                        </div>
                        
                        <label class="br-checkbox" for="sales-analytics-composite" style='margin-right:3rem;'>
                            <?php esc_html_e('Composite Chart:', 'blue-raven'); ?>
                            <input type="checkbox" name="composite" id="sales-analytics-composite" style="display: none;" checked>
                            <span class="br-check-mark" style='margin-left:1rem;'></span>
                        </label>
                    </div>
                    
                    <button type="submit" class='enterra-button'>
                        <?php esc_html_e('Update Charts', 'blue-raven'); ?>
                    </button>
                </form>
            
                <div class="stats-summary">
                    <div class="stats-block">
                        <h3><?php esc_html_e('Total Sales', 'blue-raven'); ?></h3>
                        <div class="stats-value" id="total-sales"><?php echo esc_html($total_sales ?? 0); ?></div>
                    </div>

                    <div class="stats-block">
                        <h3><?php esc_html_e('Total Revenue', 'blue-raven'); ?></h3>
                        <div class="stats-value" id="total-revenue"><?php echo esc_html('$' . number_format($total_revenue ?? 0, 2)); ?></div>
                    </div>

                    <div class="stats-block">
                        <h3><?php esc_html_e('Total Units Sold', 'blue-raven'); ?></h3>
                        <div class="stats-value" id="total-units"><?php echo esc_html($total_units ?? 0); ?></div>
                    </div>
                    <!--<div class="stats-block">-->
                    <!--    <h3><?php esc_html_e('Placeholder', 'blue-raven'); ?></h3>-->
                    <!--    <div class="stats-value">-</div>-->
                    <!--</div>-->
                </div>
                
                <div class="sales-analytics-container">
                    <div class="chart-container composite-chart-container chart-style">
                        <h2><?php esc_html_e('Composite Metrics', 'blue-raven'); ?></h2>
                        <canvas id="compositeChart"></canvas>
                    </div>
                    
                    <div class="chart-container individual-charts">
                        <div class='chart-style'> 
                            <h2><?php esc_html_e('Sales per Day', 'blue-raven'); ?></h2>
                            <canvas id="salesPerDayChart"></canvas>
                        </div>
                    </div>
                    
                    <div class="chart-container individual-charts">
                        <div class='chart-style'>
                            <h2><?php esc_html_e('Revenue per Day', 'blue-raven'); ?></h2>
                            <canvas id="revenuePerDayChart"></canvas>
                        </div>
                    </div>
                    
                    <div class="chart-container individual-charts">
                        <div class='chart-style'>
                            <h2><?php esc_html_e('Cumulative Revenue', 'blue-raven'); ?></h2>
                            <canvas id="cumulativeRevenueChart"></canvas>
                        </div>
                    </div>
                    
                    <div class="chart-container individual-charts">
                        <div class='chart-style'>
                            <h2><?php esc_html_e('Average Order Value', 'blue-raven'); ?></h2>
                            <canvas id="aovChart"></canvas>
                        </div>
                    </div>
                    
                    <div class="chart-container individual-charts">
                        <div class='chart-style'>
                            <h2><?php esc_html_e('Average Items per Order', 'blue-raven'); ?></h2>
                            <canvas id="itemsPerOrderChart"></canvas>
                        </div>
                    </div>
                    
                    <div class="chart-container">
                        <h2><?php esc_html_e('Sales by Category', 'blue-raven'); ?></h2>
                        <div  style="width: 50%; min-width: 700px; height: 400px; margin:auto; margin-bottom: 5rem; ">
                            <canvas id="salesByCategoryChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>    
        <?php
    }
    
    /**
     * Outputs the Feature Request section.
     *
     * Displays a form or list interface where users can submit, view, or manage
     * feature requests related to the Blue Raven plugin. Typically used within
     * the dashboard or a dedicated settings tab.
     *
     * @access private
     * @static
     * @return void
     */
    private static function feature_req() {
         
        ?>
        <div style='border-top: 1px solid white; margin-bottom: 2rem;'>
            <h2><?php esc_html_e('Feature Requests', 'blue-raven'); ?></h2>
        
            <p class="enterra-highlight indent-one">
                <?php esc_html_e('If you have any feature requests that you would like us to consider please log a Feature Request. We would love to get your input to help make Blue Raven even better!', 'blue-raven'); ?>
            </p>
            
            <div class='flex-right'>
                <a href="https://portal.enterrahost.com/submitticket.php?step=2&deptid=9" target="_blank" rel="noopener noreferrer" class="enterra-button" style='text-align: right; margin: 2rem 0rem 0rem auto;'>
                    <?php esc_html_e('Submit a Request', 'blue-raven'); ?>
                </a>
            </div>

            <h2><?php esc_html_e('Become a Beta Tester', 'blue-raven'); ?></h2>
        
            <p class="enterra-highlight indent-one">
                <?php esc_html_e('Join our beta team to help us improve our plugin, With limited slots, Get Blue Raven Pro for free!', 'blue-raven'); ?>
            </p>
            
            <div class='flex-right'>
                <a href="https://enterrahost.com/beta-testers/" target="_blank" rel="noopener noreferrer" class="enterra-button" style='text-align: right; margin: 2rem 0rem 0rem auto;'>
                    <?php esc_html_e('Request To Become A Beta Tester', 'blue-raven'); ?>
                </a>
            </div>
        </div>
                    
        <?php
    }
    
}
