<?php

namespace FleekDash\Services\AnalyticsTracking;

/**
 * User Agent Parser
 *
 * Parses user agent strings to extract device, browser, OS info
 *
 * @package FleekDash\Services\AnalyticsTracking
 */
class UserAgentParser
{
    /**
     * Parse user agent for device info
     *
     * @param string $user_agent User agent string
     * @return array Device type, browser, OS
     */
    public static function parse($user_agent)
    {
        $device_type = 'desktop';
        $browser = 'Unknown';
        $os = 'Unknown';

        // Simple device detection
        if (preg_match('/Mobile|Android|iPhone|iPad/', $user_agent)) {
            $device_type = preg_match('/iPad/', $user_agent) ? 'tablet' : 'mobile';
        }

        // Simple browser detection
        if (preg_match('/Chrome/', $user_agent))
            $browser = 'Chrome';
        elseif (preg_match('/Firefox/', $user_agent))
            $browser = 'Firefox';
        elseif (preg_match('/Safari/', $user_agent))
            $browser = 'Safari';
        elseif (preg_match('/Edge/', $user_agent))
            $browser = 'Edge';

        // Simple OS detection
        if (preg_match('/Windows/', $user_agent))
            $os = 'Windows';
        elseif (preg_match('/Mac/', $user_agent))
            $os = 'macOS';
        elseif (preg_match('/Linux/', $user_agent))
            $os = 'Linux';
        elseif (preg_match('/Android/', $user_agent))
            $os = 'Android';
        elseif (preg_match('/iOS/', $user_agent))
            $os = 'iOS';

        return [
            'device_type' => $device_type,
            'browser' => $browser,
            'os' => $os
        ];
    }
}

