<?php

declare(strict_types=1);

namespace App\Framework\Analytics;

/**
 * Analytics-Konfiguration
 */
final readonly class AnalyticsConfig
{
    public function __construct(
        public bool $enabled = false,
        public float $samplingRate = 1.0,
        public bool $securityAnalyticsEnabled = true,
        public string $dataPath = '/var/www/html/storage/analytics',
        public int $bufferSize = 1000,
        public int $retentionDays = 365,
        public bool $trackPageViews = true,
        public bool $trackApiCalls = true,
        public bool $trackUserActions = true,
        public bool $trackErrors = true,
        public bool $trackPerformance = true,
        public array $sensitiveKeys = [
            'password', 'token', 'api_key', 'session', 'credit_card',
            'ssn', 'personal_id', 'auth_token', 'bearer', 'cookie',
        ]
    ) {
    }

    public static function fromEnvironment(): self
    {
        return new self(
            enabled: filter_var($_ENV['ANALYTICS_ENABLED'] ?? true, FILTER_VALIDATE_BOOLEAN),
            samplingRate: (float)($_ENV['ANALYTICS_SAMPLING_RATE'] ?? 1.0),
            securityAnalyticsEnabled: filter_var($_ENV['SECURITY_ANALYTICS_ENABLED'] ?? true, FILTER_VALIDATE_BOOLEAN),
            dataPath: $_ENV['ANALYTICS_DATA_PATH'] ?? '/var/www/html/storage/analytics',
            bufferSize: (int)($_ENV['ANALYTICS_BUFFER_SIZE'] ?? 1000),
            retentionDays: (int)($_ENV['ANALYTICS_RETENTION_DAYS'] ?? 365),
            trackPageViews: filter_var($_ENV['ANALYTICS_TRACK_PAGE_VIEWS'] ?? true, FILTER_VALIDATE_BOOLEAN),
            trackApiCalls: filter_var($_ENV['ANALYTICS_TRACK_API_CALLS'] ?? true, FILTER_VALIDATE_BOOLEAN),
            trackUserActions: filter_var($_ENV['ANALYTICS_TRACK_USER_ACTIONS'] ?? true, FILTER_VALIDATE_BOOLEAN),
            trackErrors: filter_var($_ENV['ANALYTICS_TRACK_ERRORS'] ?? true, FILTER_VALIDATE_BOOLEAN),
            trackPerformance: filter_var($_ENV['ANALYTICS_TRACK_PERFORMANCE'] ?? true, FILTER_VALIDATE_BOOLEAN),
        );
    }
}
