<?php
/**
 * Live Traffic GroupBy Support
 *
 * @package VMPFence
 * @subpackage Tools
 * @since 2.1.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Class VMPFence_Live_Traffic_GroupBy
 *
 * Handles GROUP BY functionality for live traffic queries
 *
 * @since 2.1.0
 */
class VMPFence_Live_Traffic_GroupBy {

    /**
     * Group by parameter
     * @var string
     */
    private $param;

    /**
     * Display text for grouping
     * @var string
     */
    private $text;

    /**
     * Parent query reference
     * @var VMPFence_Live_Traffic_Query
     */
    private $query;

    /**
     * Valid group by parameters
     * @var array
     */
    private $validParams = array(
        'url',
        'ip',
        'user_login',
        'username',
        'statuscode',
        'action',
        'jsrun',
        'type',
        'ua',
        'referer',
    );

    /**
     * Constructor
     *
     * @since 2.1.0
     * @param VMPFence_Live_Traffic_Query $query Parent query
     * @param string $param Group by parameter
     * @param string $text Display text (optional)
     */
    public function __construct($query, $param, $text = '') {
        $this->query = $query;
        $this->param = strtolower($param);
        $this->text = $text ?: ucfirst($param);
    }

    /**
     * Validate group by parameter
     *
     * @since 2.1.0
     * @return bool
     */
    public function validate() {
        return in_array($this->param, $this->validParams);
    }

    /**
     * Get parameter
     *
     * @since 2.1.0
     * @return string
     */
    public function getParam() {
        // Handle special mappings
        if ($this->param === 'type') {
            return 'h.jsRun';
        }
        if ($this->param === 'username' || $this->param === 'user_login') {
            return 'u.user_login';
        }
        if ($this->param === 'statuscode') {
            return 'h.statusCode';
        }
        if ($this->param === 'jsrun') {
            return 'h.jsRun';
        }
        if ($this->param === 'ip') {
            return 'h.IP';
        }
        if ($this->param === 'url') {
            return 'h.URL';
        }
        if ($this->param === 'ua') {
            return 'h.UA';
        }
        if ($this->param === 'action') {
            return 'h.action';
        }
        if ($this->param === 'referer') {
            return 'h.referer';
        }

        return 'h.' . $this->param;
    }

    /**
     * Get display text
     *
     * @since 2.1.0
     * @return string
     */
    public function getText() {
        return $this->text;
    }

    /**
     * Convert to SQL GROUP BY clause
     *
     * @since 2.1.0
     * @return string SQL GROUP BY clause
     */
    public function toSQL() {
        if (!$this->validate()) {
            return '';
        }

        return $this->getParam();
    }

    /**
     * Get available group by options
     *
     * @since 2.1.0
     * @return array Available grouping options
     */
    public static function getAvailableOptions() {
        return array(
            array(
                'value' => 'type',
                'label' => 'Type (Human/Bot)',
                'description' => 'Group by visitor type'
            ),
            array(
                'value' => 'username',
                'label' => 'Username',
                'description' => 'Group by logged-in username'
            ),
            array(
                'value' => 'statuscode',
                'label' => 'HTTP Response Code',
                'description' => 'Group by HTTP status code (200, 404, etc.)'
            ),
            array(
                'value' => 'action',
                'label' => 'Firewall Response',
                'description' => 'Group by firewall action taken'
            ),
            array(
                'value' => 'ip',
                'label' => 'IP Address',
                'description' => 'Group by visitor IP address'
            ),
            array(
                'value' => 'url',
                'label' => 'URL',
                'description' => 'Group by requested URL'
            ),
            array(
                'value' => 'ua',
                'label' => 'User Agent',
                'description' => 'Group by browser/user agent'
            ),
            array(
                'value' => 'referer',
                'label' => 'Referer',
                'description' => 'Group by HTTP referer'
            ),
        );
    }
}
