<?php


class Logger {

    /*
     * Log Information to Database
     * Auto-populates: host, method, useragent, device_type, browser, os, ipaddress, location.
     * Callers may pass controller/action explicitly.
     */
    public static function Entry($logData) {
        $ua     = $_SERVER['HTTP_USER_AGENT'] ?? '';
        $parsed = self::parseUserAgent($ua);

        $logData['location']    = $logData['location']    ?? 'na';
        $logData['ipaddress']   = $logData['ipaddress']   ?: ($_SERVER['REMOTE_ADDR'] ?? '');
        $logData['host']        = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_HOST'] ?? '';
        $logData['method']      = $logData['method']      ?? ($_SERVER['REQUEST_METHOD'] ?? '');
        $logData['useragent']   = $logData['useragent']   ?? substr($ua, 0, 512);
        $logData['device_type'] = $logData['device_type'] ?? $parsed['device_type'];
        $logData['browser']     = $logData['browser']     ?? $parsed['browser'];
        $logData['os']          = $logData['os']          ?? $parsed['os'];
        // controller/action passed explicitly by caller

        return \Db::insert('sp_activitylog', $logData);
    }

    /*
     * Parse a User-Agent string into device_type, browser, os.
     */
    private static function parseUserAgent(string $ua): array {
        $u = strtolower($ua);

        // Device type
        if (preg_match('/tablet|ipad|kindle|playbook|silk|(android(?!.*mobile))/i', $ua)) {
            $device_type = 'tablet';
        } elseif (preg_match('/mobile|iphone|ipod|android.*mobile|blackberry|opera mini|iemobile/i', $ua)) {
            $device_type = 'mobile';
        } else {
            $device_type = 'desktop';
        }

        // Browser (order matters — Edge/Opera before Chrome/Safari)
        if (str_contains($u, 'edg/') || str_contains($u, 'edge/'))             $browser = 'Edge';
        elseif (str_contains($u, 'opr/') || str_contains($u, 'opera'))         $browser = 'Opera';
        elseif (str_contains($u, 'chrome') && !str_contains($u, 'chromium'))   $browser = 'Chrome';
        elseif (str_contains($u, 'firefox') || str_contains($u, 'fxios'))      $browser = 'Firefox';
        elseif (str_contains($u, 'safari') && !str_contains($u, 'chrome'))     $browser = 'Safari';
        elseif (str_contains($u, 'msie') || str_contains($u, 'trident'))       $browser = 'IE';
        else                                                                     $browser = 'Other';

        // OS
        if (str_contains($u, 'iphone') || str_contains($u, 'ipod'))  $os = 'iOS';
        elseif (str_contains($u, 'ipad'))                             $os = 'iPadOS';
        elseif (str_contains($u, 'android'))                          $os = 'Android';
        elseif (str_contains($u, 'windows'))                          $os = 'Windows';
        elseif (str_contains($u, 'macintosh') || str_contains($u, 'mac os x')) $os = 'macOS';
        elseif (str_contains($u, 'cros'))                             $os = 'ChromeOS';
        elseif (str_contains($u, 'linux'))                            $os = 'Linux';
        else                                                          $os = 'Other';

        return compact('device_type', 'browser', 'os');
    }

    /*
     * Log Information to a File
     */
    public static function LogtoFile($string){
        error_log($string, 0);
    }

} // End Class
