<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'admin_menu', 'elementsell_sales_statistics_register_menu', 21 );

/**
 * @return void
 */
function elementsell_sales_statistics_register_menu() {
    add_submenu_page(
        null,
        'Statistiky prodejů',
        'Statistiky prodejů',
        'manage_options',
        'elementsell-sales-stats',
        'elementsell_render_sales_statistics_page'
    );
}

/**
 * @return string
 */
function elementsell_get_sales_statistics_admin_url() {
    return admin_url( 'admin.php?page=elementsell-sales-stats' );
}

/**
 * @param string $orders_alias
 * @return string
 */
function elementsell_get_statistics_orders_exclusion_sql( $orders_alias = '' ) {
    if ( function_exists( 'elementsell_sales_stats_get_orders_exclusion_sql' ) ) {
        return elementsell_sales_stats_get_orders_exclusion_sql( $orders_alias );
    }

    global $wpdb;

    $prefix       = $orders_alias !== '' ? $orders_alias . '.' : '';
    $fields_table = $wpdb->prefix . 'elementsell_order_fields';

    return " AND {$prefix}id NOT IN (
        SELECT order_id
        FROM {$fields_table}
        WHERE ( field_name = 'environment' AND field_value = 'demo' )
           OR ( field_name = 'landing_demo' AND field_value = '1' )
           OR ( field_name = 'api_url' AND field_value LIKE '%demo.%' )
           OR ( field_name = 'gate_url' AND field_value LIKE '%demo.%' )
    )";
}

/**
 * @param float $amount
 * @return string
 */
function elementsell_format_sales_stat_amount( $amount ) {
    return number_format( (float) $amount, 2, ',', ' ' ) . ' Kč';
}

/**
 * @return array{revenue: float, count: int}
 */
function elementsell_get_sales_statistics_totals() {
    return elementsell_sales_stats_get_totals();
}

/**
 * @return array<string, array{label: string, total: float, items: array<int, object>}>
 */
function elementsell_get_sales_statistics_by_month() {
    return elementsell_sales_stats_get_grouped_by_month();
}

/**
 * @return array<int, object>
 */
function elementsell_get_sales_statistics_products_total() {
    return elementsell_sales_stats_get_products_total();
}

/**
 * @return void
 */
function elementsell_render_sales_statistics_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    elementsell_render_admin_styles();

    $totals        = elementsell_get_sales_statistics_totals();
    $months        = elementsell_get_sales_statistics_by_month();
    $products_total = elementsell_get_sales_statistics_products_total();

    ?>
    <div class="wrap elementsell-admin elementsell-sales-stats-page">
        <p style="margin-bottom:16px;">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=elementsell' ) ); ?>">&larr; Zpět na ElementSell</a>
        </p>

        <h1>Statistiky prodejů</h1>

        <div class="elementsell-admin-card elementsell-sales-stats-summary">
            <h2 class="elementsell-admin-section-title">Celkově zaplaceno</h2>
            <div class="elementsell-sales-stats-summary__amount">
                <?php echo esc_html( elementsell_format_sales_stat_amount( $totals['revenue'] ) ); ?>
            </div>
            <p class="elementsell-sales-stats-summary__count">
                Celkový počet všech prodejů: <strong><?php echo esc_html( number_format_i18n( $totals['count'] ) ); ?></strong>
            </p>
        </div>

        <?php if ( empty( $months ) ) : ?>
            <div class="elementsell-admin-card">
                <p class="elementsell-admin-empty">Zatím nejsou žádné zaplacené prodeje.</p>
            </div>
        <?php else : ?>
            <?php foreach ( $months as $month ) : ?>
                <div class="elementsell-admin-card elementsell-sales-stats-month">
                    <h2 class="elementsell-sales-stats-month__title">
                        <?php echo esc_html( $month['label'] ); ?>
                        <span class="elementsell-sales-stats-month__total">
                            – Celkem zaplaceno: <?php echo esc_html( elementsell_format_sales_stat_amount( $month['total'] ) ); ?>
                        </span>
                    </h2>

                    <table class="widefat striped elementsell-sales-stats-table">
                        <thead>
                            <tr>
                                <th>Produkt</th>
                                <th class="elementsell-sales-stats-table__number">Cena</th>
                                <th class="elementsell-sales-stats-table__number">Počet</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ( $month['items'] as $item ) : ?>
                                <tr>
                                    <td><?php echo esc_html( (string) $item->product_name ); ?></td>
                                    <td class="elementsell-sales-stats-table__number"><?php echo esc_html( elementsell_format_sales_stat_amount( $item->amount ) ); ?></td>
                                    <td class="elementsell-sales-stats-table__number"><?php echo esc_html( number_format_i18n( (int) $item->qty ) ); ?>x</td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>

        <div class="elementsell-admin-card elementsell-sales-stats-products-total">
            <h2 class="elementsell-admin-section-title">Prodáno produktů celkem</h2>

            <?php if ( empty( $products_total ) ) : ?>
                <p class="elementsell-admin-empty">Zatím nejsou žádné zaplacené prodeje.</p>
            <?php else : ?>
                <table class="widefat striped elementsell-sales-stats-table">
                    <thead>
                        <tr>
                            <th>Produkt</th>
                            <th class="elementsell-sales-stats-table__number">Cena</th>
                            <th class="elementsell-sales-stats-table__number">Počet</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ( $products_total as $item ) : ?>
                            <tr>
                                <td><?php echo esc_html( (string) $item->product_name ); ?></td>
                                <td class="elementsell-sales-stats-table__number"><?php echo esc_html( elementsell_format_sales_stat_amount( $item->amount ) ); ?></td>
                                <td class="elementsell-sales-stats-table__number"><?php echo esc_html( number_format_i18n( (int) $item->qty ) ); ?>x</td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
    <?php
}
