<?php
/**
 * Admin Reports page (no Recent Activity section)
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Get reports statistics within an optional date range.
 *
 * Args:
 * - date_from (Y-m-d) optional
 * - date_to   (Y-m-d) optional
 * - limit     (int) for top lists
 */
function spar_get_reports_stats( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'date_from' => '',
        'date_to'   => '',
        'limit'     => 10,
    );
    $args = wp_parse_args( $args, $defaults );

    $table      = $wpdb->prefix . 'spar_points_logs';
    $conditions = array();
    $params     = array();
    $date_from  = isset( $args['date_from'] ) ? sanitize_text_field( $args['date_from'] ) : '';
    $date_to    = isset( $args['date_to'] ) ? sanitize_text_field( $args['date_to'] ) : '';
    if ( $date_from && strtotime( $date_from ) ) {
        $conditions[] = ' AND date >= %s';
        $params[]     = $date_from . ' 00:00:00';
    }
    if ( $date_to && strtotime( $date_to ) ) {
        $conditions[] = ' AND date <= %s';
        $params[]     = $date_to . ' 23:59:59';
    }
    $where_dynamic = implode( '', $conditions );

    $cache_group = 'spar';

    // Totals: consider both sign and legacy type to ensure compatibility with all data
    $totals_sql_base = "SELECT 
        SUM(CASE WHEN (points > 0 OR type = 'add') THEN points ELSE 0 END) AS earned,
        SUM(CASE WHEN (points < 0 OR type = 'remove') THEN ABS(points) ELSE 0 END) AS redeemed,
        COUNT(*) AS activity,
        COUNT(DISTINCT user_id) AS customers
        FROM %i WHERE 1=1" . $where_dynamic;
    $totals_args      = array_merge( array( $totals_sql_base, $table ), $params );
    $totals_sql       = call_user_func_array( array( $wpdb, 'prepare' ), $totals_args );

    $totals_key = 'spar_reports_totals_' . md5( $totals_sql );
    $totals = wp_cache_get( $totals_key, $cache_group );
    if ( false === $totals ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $totals = $wpdb->get_row( $totals_sql, ARRAY_A );
        if ( ! is_array( $totals ) ) {
            $totals = array( 'earned' => 0, 'redeemed' => 0, 'activity' => 0, 'customers' => 0 );
        }
        wp_cache_set( $totals_key, $totals, $cache_group, 60 );
    }

    $earned    = (int) ( $totals['earned'] ?? 0 );
    $redeemed  = (int) ( $totals['redeemed'] ?? 0 );
    $activity  = (int) ( $totals['activity'] ?? 0 );
    $customers = (int) ( $totals['customers'] ?? 0 );

    // Top earners: points positive or legacy type add
    $limit = max( 1, absint( $args['limit'] ) );
    $top_earners_sql_base = 'SELECT user_id, SUM(CASE WHEN (points > 0 OR type = %s) THEN points ELSE 0 END) AS total_points FROM %i WHERE 1=1' . $where_dynamic . ' GROUP BY user_id HAVING total_points > 0 ORDER BY total_points DESC LIMIT %d';
    $top_earners_args      = array_merge( array( $top_earners_sql_base, 'add', $table ), $params, array( $limit ) );
    $top_earners_sql       = call_user_func_array( array( $wpdb, 'prepare' ), $top_earners_args );
    $top_earners_key = 'spar_reports_top_earners_' . md5( $top_earners_sql );
    $top_earners = wp_cache_get( $top_earners_key, $cache_group );
    if ( false === $top_earners ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $top_earners = $wpdb->get_results( $top_earners_sql, ARRAY_A );
        wp_cache_set( $top_earners_key, $top_earners, $cache_group, 60 );
    }

    // Top redeemers: points negative or legacy type remove (ABS for magnitude)
    $top_redeemers_sql_base = 'SELECT user_id, SUM(CASE WHEN (points < 0 OR type = %s) THEN ABS(points) ELSE 0 END) AS total_points FROM %i WHERE 1=1' . $where_dynamic . ' GROUP BY user_id HAVING total_points > 0 ORDER BY total_points DESC LIMIT %d';
    $top_redeemers_args      = array_merge( array( $top_redeemers_sql_base, 'remove', $table ), $params, array( $limit ) );
    $top_redeemers_sql       = call_user_func_array( array( $wpdb, 'prepare' ), $top_redeemers_args );
    $top_redeemers_key = 'spar_reports_top_redeemers_' . md5( $top_redeemers_sql );
    $top_redeemers = wp_cache_get( $top_redeemers_key, $cache_group );
    if ( false === $top_redeemers ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $top_redeemers = $wpdb->get_results( $top_redeemers_sql, ARRAY_A );
        wp_cache_set( $top_redeemers_key, $top_redeemers, $cache_group, 60 );
    }

    return array(
        'earned'        => $earned,
        'redeemed'      => $redeemed,
        'net'           => max( 0, $earned - $redeemed ),
        'activity'      => $activity,
        'customers'     => $customers,
        'top_earners'   => $top_earners,
        'top_redeemers' => $top_redeemers,
    );
}

/**
 * Get referral statistics for the admin reports.
 * - Clicks are aggregated from usermeta (all-time)
 * - Conversions/points/top referrers are derived from logs and respect date range
 */
function spar_get_referral_reports( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'date_from' => '',
        'date_to'   => '',
        'limit'     => 10,
    );
    $args = wp_parse_args( $args, $defaults );

    $table      = $wpdb->prefix . 'spar_points_logs';
    $conds      = array();
    $params     = array();
    $date_from  = isset( $args['date_from'] ) ? sanitize_text_field( $args['date_from'] ) : '';
    $date_to    = isset( $args['date_to'] ) ? sanitize_text_field( $args['date_to'] ) : '';
    if ( $date_from && strtotime( $date_from ) ) {
        $conds[]  = ' AND date >= %s';
        $params[] = $date_from . ' 00:00:00';
    }
    if ( $date_to && strtotime( $date_to ) ) {
        $conds[]  = ' AND date <= %s';
        $params[] = $date_to . ' 23:59:59';
    }
    $where_dynamic = implode( '', $conds );

    $cache_group = 'spar';

    // Identify referral award rows by action prefix using the current configured name.
    $options       = get_option( 'spar_options', array() );
    $action_name   = isset( $options['earn']['referral']['name'] ) && $options['earn']['referral']['name'] !== ''
    ? sanitize_text_field( $options['earn']['referral']['name'] )
    : esc_html__( 'Referral Bonus', 'simple-points-and-rewards' );
    // Ensure a colon suffix is included in the LIKE pattern, as logs are written like "{name}: ..."
    $ref_action_like = $action_name . ':%';
    $default_action_like = esc_html__( 'Referral Bonus', 'simple-points-and-rewards' ) . ':%';
    $use_dual_like = ( $ref_action_like !== $default_action_like );

    // Conversions in range (count referral award log entries)
    if ( $use_dual_like ) {
        $conv_sql_base = 'SELECT COUNT(*) FROM %i WHERE 1=1' . $where_dynamic . ' AND (action LIKE %s OR action LIKE %s) AND (points > 0 OR type = %s)';
        $conv_args     = array_merge( array( $conv_sql_base, $table, $ref_action_like, $default_action_like, 'add' ), $params );
        $conversions_sql = call_user_func_array( array( $wpdb, 'prepare' ), $conv_args );
    } else {
        $conv_sql_base = 'SELECT COUNT(*) FROM %i WHERE 1=1' . $where_dynamic . ' AND action LIKE %s AND (points > 0 OR type = %s)';
        $conv_args     = array_merge( array( $conv_sql_base, $table, $ref_action_like, 'add' ), $params );
        $conversions_sql = call_user_func_array( array( $wpdb, 'prepare' ), $conv_args );
    }
    $conv_key = 'spar_reports_ref_conversions_' . md5( $conversions_sql );
    $conversions = wp_cache_get( $conv_key, $cache_group );
    if ( false === $conversions ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $conversions = (int) $wpdb->get_var( $conversions_sql );
        wp_cache_set( $conv_key, $conversions, $cache_group, 60 );
    }

    // Points from referrals in range
    if ( $use_dual_like ) {
        $points_sql_base = 'SELECT SUM(points) FROM %i WHERE 1=1' . $where_dynamic . ' AND (action LIKE %s OR action LIKE %s) AND (points > 0 OR type = %s)';
        $points_args     = array_merge( array( $points_sql_base, $table, $ref_action_like, $default_action_like, 'add' ), $params );
        $points_sql      = call_user_func_array( array( $wpdb, 'prepare' ), $points_args );
    } else {
        $points_sql_base = 'SELECT SUM(points) FROM %i WHERE 1=1' . $where_dynamic . ' AND action LIKE %s AND (points > 0 OR type = %s)';
        $points_args     = array_merge( array( $points_sql_base, $table, $ref_action_like, 'add' ), $params );
        $points_sql      = call_user_func_array( array( $wpdb, 'prepare' ), $points_args );
    }
    $points_key = 'spar_reports_ref_points_' . md5( $points_sql );
    $points_total = wp_cache_get( $points_key, $cache_group );
    if ( false === $points_total ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $points_total = (int) $wpdb->get_var( $points_sql );
        wp_cache_set( $points_key, $points_total, $cache_group, 60 );
    }

    // Top referrers in range (by points and count)
    $limit = max( 1, absint( $args['limit'] ) );
    if ( $use_dual_like ) {
        $top_sql_base = 'SELECT user_id, COUNT(*) AS referrals, SUM(points) AS total_points FROM %i WHERE 1=1' . $where_dynamic . ' AND (action LIKE %s OR action LIKE %s) AND (points > 0 OR type = %s) GROUP BY user_id HAVING total_points > 0 ORDER BY total_points DESC, referrals DESC LIMIT %d';
        $top_args      = array_merge( array( $top_sql_base, $table, $ref_action_like, $default_action_like, 'add', $limit ), $params );
        $top_sql       = call_user_func_array( array( $wpdb, 'prepare' ), $top_args );
    } else {
        $top_sql_base = 'SELECT user_id, COUNT(*) AS referrals, SUM(points) AS total_points FROM %i WHERE 1=1' . $where_dynamic . ' AND action LIKE %s AND (points > 0 OR type = %s) GROUP BY user_id HAVING total_points > 0 ORDER BY total_points DESC, referrals DESC LIMIT %d';
        $top_args      = array_merge( array( $top_sql_base, $table, $ref_action_like, 'add', $limit ), $params );
        $top_sql       = call_user_func_array( array( $wpdb, 'prepare' ), $top_args );
    }
    $top_key = 'spar_reports_ref_top_' . md5( $top_sql );
    $top_referrers = wp_cache_get( $top_key, $cache_group );
    if ( false === $top_referrers ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $top_referrers = $wpdb->get_results( $top_sql, ARRAY_A );
        wp_cache_set( $top_key, $top_referrers, $cache_group, 60 );
    }

    // Clicks all-time from usermeta (serialized array per user)
    $clicks_all_time = 0;
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $meta_rows = $wpdb->get_col( $wpdb->prepare( 'SELECT meta_value FROM %i WHERE meta_key = %s', $wpdb->usermeta, 'spar_referral_stats' ) );
    if ( ! empty( $meta_rows ) ) {
        foreach ( $meta_rows as $val ) {
            $stats = maybe_unserialize( $val );
            if ( is_array( $stats ) && isset( $stats['total_clicks'] ) ) {
                $clicks_all_time += (int) $stats['total_clicks'];
            }
        }
    }

    return array(
        'clicks_all_time'   => (int) $clicks_all_time,
        'conversions_range' => (int) $conversions,
        'points_range'      => (int) max( 0, $points_total ),
        'top_referrers'     => $top_referrers,
    );
}

/**
 * Get total earned points grouped by earning method (action_id) within an optional date range.
 * Falls back to best-effort grouping from legacy action text when action_id is empty.
 *
 * Returns array of rows: [ [ 'method' => 'order', 'label' => 'Orders', 'points' => 1234, 'count' => 56, 'icon' => 'fa-cart-shopping' ], ... ]
 */
function spar_get_points_per_method( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'date_from' => '',
        'date_to'   => '',
        'limit'     => 20, // show many; UI can decide how many to render
    );
    $args = wp_parse_args( $args, $defaults );

    $table      = $wpdb->prefix . 'spar_points_logs';
    $conds      = array();
    $params     = array();
    $date_from  = isset( $args['date_from'] ) ? sanitize_text_field( $args['date_from'] ) : '';
    $date_to    = isset( $args['date_to'] ) ? sanitize_text_field( $args['date_to'] ) : '';
    if ( $date_from && strtotime( $date_from ) ) { $conds[] = ' AND date >= %s'; $params[] = $date_from . ' 00:00:00'; }
    if ( $date_to && strtotime( $date_to ) ) { $conds[] = ' AND date <= %s'; $params[] = $date_to . ' 23:59:59'; }
    $conds[] = " AND (points > 0 OR type = 'add')"; // static fragment
    $where_dynamic = implode( '', $conds );
    $sql_base = "SELECT 
            CASE WHEN action_id IS NULL OR action_id = '' THEN '__legacy__' ELSE action_id END AS method,
            COUNT(*) AS count_events,
            SUM(points) AS total_points
        FROM %i WHERE 1=1" . $where_dynamic . '
        GROUP BY method
        ORDER BY total_points DESC';
    $sql_args = array_merge( array( $sql_base, $table ), $params );
    $sql      = call_user_func_array( array( $wpdb, 'prepare' ), $sql_args );

    $cache_group = 'spar';
    $cache_key   = 'spar_reports_points_per_method_' . md5( $sql );
    $rows = wp_cache_get( $cache_key, $cache_group );
    if ( false === $rows ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $rows = $wpdb->get_results( $sql, ARRAY_A );
        wp_cache_set( $cache_key, $rows, $cache_group, 60 );
    }

    // Map known methods to labels/icons. Allow filters for customization.
    $points_label = apply_filters( 'spar_points_label', esc_html__( 'Points', 'simple-points-and-rewards' ) );
    $map = array(
    'signup'          => array( 'label' => esc_html__( 'Signups', 'simple-points-and-rewards' ),        'icon' => 'fa-user-plus' ),
    'order'           => array( 'label' => esc_html__( 'Orders', 'simple-points-and-rewards' ),          'icon' => 'fa-cart-shopping' ),
    'first_order'     => array( 'label' => esc_html__( 'First Order Bonus', 'simple-points-and-rewards' ), 'icon' => 'fa-flag' ),
    'nth_order'       => array( 'label' => esc_html__( 'Nth Order Bonus', 'simple-points-and-rewards' ),   'icon' => 'fa-ranking-star' ),
    'referral'        => array( 'label' => esc_html__( 'Referrals', 'simple-points-and-rewards' ),       'icon' => 'fa-user-group' ),
    'review'          => array( 'label' => esc_html__( 'Reviews', 'simple-points-and-rewards' ),         'icon' => 'fa-star' ),
    'admin_adjustment'=> array( 'label' => esc_html__( 'Admin Adjustments', 'simple-points-and-rewards' ), 'icon' => 'fa-screwdriver-wrench' ),
        // Negative actions like 'redeem', 'order_refund' intentionally omitted from earned view
    '__other__'       => array( 'label' => esc_html__( 'Other', 'simple-points-and-rewards' ),           'icon' => 'fa-gift' ),
    );
    $map = apply_filters( 'spar_points_method_map', $map, $points_label );

    // Determine which methods are enabled in settings
    $options = get_option( 'spar_options', array() );
    $earn    = isset( $options['earn'] ) && is_array( $options['earn'] ) ? $options['earn'] : array();
    $enabled_methods = array();
    if ( ! empty( $earn['signup']['enabled'] ) ) {
        $enabled_methods['signup'] = true;
    }
    if ( ! empty( $earn['order']['enabled'] ) || ! empty( $earn['order_fixed']['enabled'] ) ) {
        $enabled_methods['order'] = true; // both mechanisms contribute to action_id "order"
    }
    if ( ! empty( $earn['first_order']['enabled'] ) ) {
        $enabled_methods['first_order'] = true;
    }
    if ( ! empty( $earn['nth_order']['enabled'] ) ) {
        $enabled_methods['nth_order'] = true;
    }
    if ( ! empty( $earn['referral']['enabled'] ) ) {
        $enabled_methods['referral'] = true;
    }
    if ( ! empty( $earn['review']['enabled'] ) ) { // premium
        $enabled_methods['review'] = true;
    }
    /**
     * Filter the enabled methods map used to decide which cards to show.
     * Keys are method slugs (e.g., 'order', 'signup'). Values are truthy to enable.
     */
    $enabled_methods = apply_filters( 'spar_points_method_enabled_map', $enabled_methods, $options );

    // For legacy rows (no action_id), attempt to infer a method from the action text
    $infer_method_from_action = function( $action_text ) {
        $t = is_string( $action_text ) ? strtolower( $action_text ) : '';
        if ( $t === '' ) { return '__other__'; }
        if ( strpos( $t, 'signup' ) !== false ) { return 'signup'; }
        if ( strpos( $t, 'order' ) !== false ) { return 'order'; }
        if ( strpos( $t, 'referr' ) !== false ) { return 'referral'; }
        if ( strpos( $t, 'review' ) !== false ) { return 'review'; }
        if ( strpos( $t, 'admin' ) !== false ) { return 'admin_adjustment'; }
        return '__other__';
    };

    // If there is a legacy bucket, split it further by inspecting action text.
    $has_legacy = false;
    foreach ( (array) $rows as $r ) {
        if ( isset( $r['method'] ) && '__legacy__' === $r['method'] ) { $has_legacy = true; break; }
    }
    if ( $has_legacy ) {
        // Query all legacy rows for this date range to classify
    $legacy_sql_base = 'SELECT action, COUNT(*) AS cnt, SUM(points) AS pts FROM %i WHERE 1=1' . $where_dynamic . " AND (action_id IS NULL OR action_id = '') GROUP BY action";
    $legacy_sql      = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $legacy_sql_base, $table ), $params ) );
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $legacy_rows = $wpdb->get_results( $legacy_sql, ARRAY_A );
        $buckets = array();
        foreach ( (array) $legacy_rows as $lr ) {
            $method = $infer_method_from_action( $lr['action'] ?? '' );
            if ( ! isset( $buckets[ $method ] ) ) {
                $buckets[ $method ] = array( 'count_events' => 0, 'total_points' => 0 );
            }
            $buckets[ $method ]['count_events'] += (int) ( $lr['cnt'] ?? 0 );
            $buckets[ $method ]['total_points'] += (int) ( $lr['pts'] ?? 0 );
        }
        // Rebuild rows without the __legacy__ placeholder
        $new_rows = array();
        foreach ( (array) $rows as $r ) {
            if ( isset( $r['method'] ) && '__legacy__' === $r['method'] ) {
                continue;
            }
            $new_rows[] = $r;
        }
        foreach ( $buckets as $method => $agg ) {
            $new_rows[] = array(
                'method'       => $method,
                'count_events' => (int) $agg['count_events'],
                'total_points' => (int) $agg['total_points'],
            );
        }
        // Sort desc by points
        usort( $new_rows, function( $a, $b ) {
            $ap = isset( $a['total_points'] ) ? (int) $a['total_points'] : 0;
            $bp = isset( $b['total_points'] ) ? (int) $b['total_points'] : 0;
            if ( $ap === $bp ) { return 0; }
            return ( $ap > $bp ) ? -1 : 1;
        } );
        $rows = $new_rows;
    }

    // Build a lookup of present data by method
    $present = array();
    foreach ( (array) $rows as $r ) {
        $m = sanitize_key( (string) ( $r['method'] ?? '' ) );
        if ( ! $m ) { $m = '__other__'; }
        $present[ $m ] = array(
            'points' => (int) ( $r['total_points'] ?? 0 ),
            'count'  => (int) ( $r['count_events'] ?? 0 ),
        );
    }

    // Normalize into final shape with labels/icons and include zeros for known methods
    $out = array();
    foreach ( $map as $method_key => $meta ) {
        if ( '__other__' === $method_key ) {
            continue; // handle unknowns after
        }
        // Only include if enabled in settings
        if ( empty( $enabled_methods[ $method_key ] ) ) {
            continue;
        }
        $data = $present[ $method_key ] ?? array( 'points' => 0, 'count' => 0 );
        $out[] = array(
            'method' => $method_key,
            'label'  => (string) $meta['label'],
            'icon'   => (string) $meta['icon'],
            'points' => (int) $data['points'],
            'count'  => (int) $data['count'],
        );
    }

    // Optionally append any additional methods not in the map (use generic formatting)
    $show_unknown = (bool) apply_filters( 'spar_points_per_method_show_unknown', false, $present, $map, $args );
    if ( $show_unknown ) {
        foreach ( $present as $method_key => $data ) {
            if ( isset( $map[ $method_key ] ) || '__legacy__' === $method_key ) {
                continue;
            }
            $label_guess = ucwords( str_replace( array( '-', '_' ), ' ', $method_key ) );
            $fallback = $map['__other__'];
            $out[] = array(
                'method' => $method_key,
                'label'  => $label_guess ? $label_guess : (string) $fallback['label'],
                'icon'   => (string) $fallback['icon'],
                'points' => (int) $data['points'],
                'count'  => (int) $data['count'],
            );
        }
    }

    // Sort by points desc so non-zero appear first, but zeros remain visible
    usort( $out, function( $a, $b ) {
        $ap = isset( $a['points'] ) ? (int) $a['points'] : 0;
        $bp = isset( $b['points'] ) ? (int) $b['points'] : 0;
        if ( $ap === $bp ) { return 0; }
        return ( $ap > $bp ) ? -1 : 1;
    } );

    /**
     * Filter the computed points-per-method rows before rendering.
     *
     * @param array $out  Array of rows with method, label, icon, points, count.
     * @param array $args Input args including date range.
     */
    $out = apply_filters( 'spar_points_per_method_rows', $out, $args );
    return $out;
}

/**
 * Render the Admin Reports page
 */
function spar_reports_admin_page() {
    // Capability check (defense in depth)
    if ( ! current_user_can( 'manage_woocommerce' ) ) {
        wp_die( esc_html__( 'You do not have permission to view this page.', 'simple-points-and-rewards' ) );
    }
    $points_label = apply_filters( 'spar_points_label', esc_html__( 'Points', 'simple-points-and-rewards' ) );

    // Process filters (GET)
    $date_from = isset( $_GET['spar_from'] ) ? sanitize_text_field( wp_unslash( $_GET['spar_from'] ) ) : '';
    $date_to   = isset( $_GET['spar_to'] ) ? sanitize_text_field( wp_unslash( $_GET['spar_to'] ) ) : '';

    // Verify nonce for filters; if invalid, ignore provided dates
    $has_valid_nonce = isset( $_GET['spar_reports_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['spar_reports_nonce'] ) ), 'spar_reports_filter' );
    if ( ! $has_valid_nonce ) {
        $date_from = '';
        $date_to   = '';
    }

    // Basic YYYY-MM-DD validation; else reset
    if ( $date_from && ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_from ) ) {
        $date_from = '';
    }
    if ( $date_to && ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_to ) ) {
        $date_to = '';
    }

    $stats = spar_get_reports_stats( array(
        'date_from' => $date_from,
        'date_to'   => $date_to,
        'limit'     => 10,
    ) );

    if ( function_exists( 'spar_render_admin_header' ) ) {
    spar_render_admin_header( esc_html__( 'Reports', 'simple-points-and-rewards' ) );
    }
    ?>
    <div class="wrap spar-reports-wrap">
        <form method="get" action="" class="spar-reports-filters" style="margin: 12px 0 16px;">
            <input type="hidden" name="page" value="spar-reports" />
            <?php wp_nonce_field( 'spar_reports_filter', 'spar_reports_nonce', false ); ?>
            <label for="spar_from" class="screen-reader-text"><?php esc_html_e( 'From date', 'simple-points-and-rewards' ); ?></label>
            <input type="date" id="spar_from" name="spar_from" value="<?php echo esc_attr( $date_from ); ?>" />

            <label for="spar_to" class="screen-reader-text"><?php esc_html_e( 'To date', 'simple-points-and-rewards' ); ?></label>
            <input type="date" id="spar_to" name="spar_to" value="<?php echo esc_attr( $date_to ); ?>" />

            <div class="actions">
                <button type="submit" class="button button-primary"><?php esc_html_e( 'Apply', 'simple-points-and-rewards' ); ?></button>
                <a href="<?php echo esc_url( remove_query_arg( array( 'spar_from', 'spar_to', 'spar_reports_nonce', '_wp_http_referer' ) ) ); ?>" class="button"><?php esc_html_e( 'Reset', 'simple-points-and-rewards' ); ?></a>
            </div>
        </form>

        <h2><?php esc_html_e( 'General Analytics', 'simple-points-and-rewards' ); ?></h2>

        <div class="spar-reports-cards" style="margin-bottom: 16px;">
            <div class="spar-card is-earned">
                <div class="spar-card-icon"><i class="fa-solid fa-arrow-up-right-dots" aria-hidden="true"></i></div>
                <div class="spar-card-content">
                    <div class="spar-card-label"><?php esc_html_e( 'Total Earned', 'simple-points-and-rewards' ); ?></div>
                    <div class="spar-card-value">+<?php echo esc_html( number_format_i18n( (int) $stats['earned'] ) ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></div>
                </div>
            </div>
            <div class="spar-card is-redeemed">
                <div class="spar-card-icon"><i class="fa-solid fa-arrow-down" aria-hidden="true"></i></div>
                <div class="spar-card-content">
                    <div class="spar-card-label"><?php esc_html_e( 'Total Redeemed', 'simple-points-and-rewards' ); ?></div>
                    <div class="spar-card-value">-<?php echo esc_html( number_format_i18n( (int) $stats['redeemed'] ) ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></div>
                </div>
            </div>
            <div class="spar-card is-net">
                <div class="spar-card-icon"><i class="fa-solid fa-scale-balanced" aria-hidden="true"></i></div>
                <div class="spar-card-content">
                    <div class="spar-card-label"><?php esc_html_e( 'Net Points', 'simple-points-and-rewards' ); ?></div>
                    <div class="spar-card-value"><?php echo esc_html( number_format_i18n( (int) $stats['net'] ) ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></div>
                </div>
            </div>
            <div class="spar-card is-activity">
                <div class="spar-card-icon"><i class="fa-solid fa-list-check" aria-hidden="true"></i></div>
                <div class="spar-card-content">
                    <div class="spar-card-label"><?php esc_html_e( 'Activities', 'simple-points-and-rewards' ); ?></div>
                    <div class="spar-card-value"><?php echo esc_html( number_format_i18n( (int) $stats['activity'] ) ); ?></div>
                </div>
            </div>
            <div class="spar-card is-customers">
                <div class="spar-card-icon"><i class="fa-solid fa-users" aria-hidden="true"></i></div>
                <div class="spar-card-content">
                    <div class="spar-card-label"><?php esc_html_e( 'Active Customers', 'simple-points-and-rewards' ); ?></div>
                    <div class="spar-card-value"><?php echo esc_html( number_format_i18n( (int) $stats['customers'] ) ); ?></div>
                </div>
            </div>
        </div>

        <?php
        // Points Per Method section
        $ppm_rows = spar_get_points_per_method( array( 'date_from' => $date_from, 'date_to' => $date_to ) ); ?>
        <div class="spar-report-table" style="margin-top:8px; grid-column: 1 / -1;">
            <h2 style="margin-top: 29px;"><?php esc_html_e( 'Points Per Method', 'simple-points-and-rewards' ); ?></h2>
            <div class="spar-reports-cards">
                <?php foreach ( $ppm_rows as $row ) :
                    $label  = isset( $row['label'] ) ? $row['label'] : '';
                    $points = isset( $row['points'] ) ? (int) $row['points'] : 0;
                    $icon   = isset( $row['icon'] ) ? $row['icon'] : 'fa-gift';
                ?>
                <div class="spar-card is-earned">
                    <div class="spar-card-icon"><i class="fa-solid <?php echo esc_attr( $icon ); ?>" aria-hidden="true"></i></div>
                    <div class="spar-card-content">
                        <div class="spar-card-label"><?php echo esc_html( $label ); ?></div>
                        <div class="spar-card-value">+<?php echo esc_html( number_format_i18n( (int) $points ) ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        

        <div class="spar-reports-tables">
            <div class="spar-report-table">
                <h2><?php esc_html_e( 'Top Earners', 'simple-points-and-rewards' ); ?></h2>
                <div class="spar-table-wrap">
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'User', 'simple-points-and-rewards' ); ?></th>
                                <th style="text-align:right;">+<?php echo esc_html( $points_label ); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ( ! empty( $stats['top_earners'] ) ) :
                                foreach ( $stats['top_earners'] as $row ) :
                                    $user = get_userdata( (int) $row['user_id'] );
                                    $user_name = $user ? $user->display_name : esc_html__( 'Unknown', 'simple-points-and-rewards' );
                                    $user_link = '';
                                    if ( $user ) {
                                        $user_link = add_query_arg(
                                            array(
                                                    'page'    => 'spar-customer',
                                                'user_id' => (int) $user->ID,
                                                'nonce'   => wp_create_nonce( 'spar_customer_detail_view_' . (int) $user->ID ),
                                            ),
                                            admin_url( 'admin.php' )
                                        );
                                    }
                                    ?>
                                    <tr>
                                        <td>
                                            <?php if ( $user_link ) : ?>
                                                <a href="<?php echo esc_url( $user_link ); ?>"><?php echo esc_html( $user_name ); ?></a>
                                            <?php else : ?>
                                                <?php echo esc_html( $user_name ); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td style="text-align:right;">+<?php echo esc_html( number_format_i18n( (int) $row['total_points'] ) ); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <tr><td colspan="2"><?php esc_html_e( 'No data found for this range.', 'simple-points-and-rewards' ); ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="spar-report-table">
                <h2><?php esc_html_e( 'Top Redeemers', 'simple-points-and-rewards' ); ?></h2>
                <div class="spar-table-wrap">
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'User', 'simple-points-and-rewards' ); ?></th>
                                <th style="text-align:right;">-<?php echo esc_html( $points_label ); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ( ! empty( $stats['top_redeemers'] ) ) :
                                foreach ( $stats['top_redeemers'] as $row ) :
                                    $user = get_userdata( (int) $row['user_id'] );
                                    $user_name = $user ? $user->display_name : esc_html__( 'Unknown', 'simple-points-and-rewards' );
                                    $user_link = '';
                                    if ( $user ) {
                                        $user_link = add_query_arg(
                                            array(
                                                    'page'    => 'spar-customer',
                                                'user_id' => (int) $user->ID,
                                                'nonce'   => wp_create_nonce( 'spar_customer_detail_view_' . (int) $user->ID ),
                                            ),
                                            admin_url( 'admin.php' )
                                        );
                                    }
                                    ?>
                                    <tr>
                                        <td>
                                            <?php if ( $user_link ) : ?>
                                                <a href="<?php echo esc_url( $user_link ); ?>"><?php echo esc_html( $user_name ); ?></a>
                                            <?php else : ?>
                                                <?php echo esc_html( $user_name ); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td style="text-align:right;">-<?php echo esc_html( number_format_i18n( (int) $row['total_points'] ) ); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else : ?>
                                <tr><td colspan="2"><?php esc_html_e( 'No data found for this range.', 'simple-points-and-rewards' ); ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php $ref_stats = spar_get_referral_reports( array( 'date_from' => $date_from, 'date_to' => $date_to, 'limit' => 10 ) ); ?>
        <div class="spar-reports-tables" style="margin-top:24px;">
            <div class="spar-report-table" style="grid-column: 1 / -1;">
                <h2><?php esc_html_e( 'Referral Statistics', 'simple-points-and-rewards' ); ?></h2>
                <div class="spar-reports-cards">
                    <div class="spar-card is-activity">
                        <div class="spar-card-icon"><i class="fa-solid fa-mouse-pointer" aria-hidden="true"></i></div>
                        <div class="spar-card-content">
                            <div class="spar-card-label"><?php esc_html_e( 'Referral Clicks (All-time)', 'simple-points-and-rewards' ); ?></div>
                            <div class="spar-card-value"><?php echo esc_html( number_format_i18n( (int) $ref_stats['clicks_all_time'] ) ); ?></div>
                        </div>
                    </div>
                    <div class="spar-card is-earned">
                        <div class="spar-card-icon"><i class="fa-solid fa-user-plus" aria-hidden="true"></i></div>
                        <div class="spar-card-content">
                            <div class="spar-card-label"><?php esc_html_e( 'Successful Referrals (Range)', 'simple-points-and-rewards' ); ?></div>
                            <div class="spar-card-value"><?php echo esc_html( number_format_i18n( (int) $ref_stats['conversions_range'] ) ); ?></div>
                        </div>
                    </div>
                    <div class="spar-card is-net">
                        <div class="spar-card-icon"><i class="fa-solid fa-coins" aria-hidden="true"></i></div>
                        <div class="spar-card-content">
                            <div class="spar-card-label"><?php esc_html_e( 'Referral Points (Range)', 'simple-points-and-rewards' ); ?></div>
                            <div class="spar-card-value">+<?php echo esc_html( number_format_i18n( (int) $ref_stats['points_range'] ) ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></div>
                        </div>
                    </div>
                </div>

                <h3 style="margin-top:10px;">
                    <?php esc_html_e( 'Top Referrers (by points in range)', 'simple-points-and-rewards' ); ?>
                </h3>
                <div class="spar-table-wrap">
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'User', 'simple-points-and-rewards' ); ?></th>
                                <th style="text-align:right;">+<?php esc_html_e( 'Referral', 'simple-points-and-rewards' ); ?> <?php echo esc_html( strtolower( $points_label ) ); ?></th>
                                <th style="text-align:right;">&nbsp;<?php esc_html_e( 'Referrals', 'simple-points-and-rewards' ); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ( ! empty( $ref_stats['top_referrers'] ) ) : foreach ( $ref_stats['top_referrers'] as $row ) :
                                $user = get_userdata( (int) $row['user_id'] );
                                $user_name = $user ? $user->display_name : esc_html__( 'Unknown', 'simple-points-and-rewards' );
                                $user_link = '';
                                if ( $user ) {
                                    $user_link = add_query_arg(
                                        array(
                                                'page'    => 'spar-customer',
                                            'user_id' => (int) $user->ID,
                                            'nonce'   => wp_create_nonce( 'spar_customer_detail_view_' . (int) $user->ID ),
                                        ),
                                        admin_url( 'admin.php' )
                                    );
                                }
                                ?>
                                <tr>
                                    <td>
                                        <?php if ( $user_link ) : ?>
                                            <a href="<?php echo esc_url( $user_link ); ?>"><?php echo esc_html( $user_name ); ?></a>
                                        <?php else : ?>
                                            <?php echo esc_html( $user_name ); ?>
                                        <?php endif; ?>
                                    </td>
                                    <td style="text-align:right;">+<?php echo esc_html( number_format_i18n( (int) $row['total_points'] ) ); ?></td>
                                    <td style="text-align:right;"><?php echo esc_html( number_format_i18n( (int) $row['referrals'] ) ); ?></td>
                                </tr>
                            <?php endforeach; else : ?>
                                <tr><td colspan="3"><?php esc_html_e( 'No referral data found for this range.', 'simple-points-and-rewards' ); ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>
    <?php
}
