-- Clean Architecture Database Schema v2
-- Erweitert das bestehende Schema um neue Tabellen

-- Session Management
CREATE TABLE IF NOT EXISTS browser_sessions (
    id TEXT PRIMARY KEY,
    fingerprint_id TEXT NOT NULL,
    cookies TEXT NOT NULL, -- JSON encrypted
    local_storage TEXT, -- JSON encrypted
    session_storage TEXT, -- JSON encrypted
    proxy_config TEXT, -- JSON
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_used TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    health_score REAL DEFAULT 1.0,
    account_id TEXT,
    user_agent TEXT,
    viewport_width INTEGER DEFAULT 1920,
    viewport_height INTEGER DEFAULT 1080,
    locale TEXT DEFAULT 'de-DE',
    timezone TEXT DEFAULT 'Europe/Berlin',
    active BOOLEAN DEFAULT 1,
    error_count INTEGER DEFAULT 0,
    success_count INTEGER DEFAULT 0,
    FOREIGN KEY (fingerprint_id) REFERENCES browser_fingerprints(id),
    FOREIGN KEY (account_id) REFERENCES accounts(id)
);

-- Fingerprints
CREATE TABLE IF NOT EXISTS browser_fingerprints (
    id TEXT PRIMARY KEY,
    canvas_noise_config TEXT NOT NULL, -- JSON
    webrtc_config TEXT NOT NULL, -- JSON
    fonts TEXT NOT NULL, -- JSON array
    hardware_config TEXT NOT NULL, -- JSON
    navigator_props TEXT NOT NULL, -- JSON
    webgl_vendor TEXT,
    webgl_renderer TEXT,
    audio_context_config TEXT, -- JSON
    timezone TEXT,
    timezone_offset INTEGER,
    plugins TEXT, -- JSON array
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_rotated TIMESTAMP,
    platform_specific TEXT, -- Platform-spezifische Anpassungen
    static_components TEXT, -- JSON: Unchangeable hardware/platform values
    rotation_seed TEXT, -- Seed for deterministic noise generation
    account_bound BOOLEAN DEFAULT 0 -- Is this bound to specific account(s)
);

-- Rate Limiting
CREATE TABLE IF NOT EXISTS rate_limit_events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    action_type TEXT NOT NULL,
    duration_ms INTEGER NOT NULL,
    success BOOLEAN NOT NULL,
    response_code INTEGER,
    session_id TEXT,
    url TEXT,
    element_selector TEXT,
    error_message TEXT,
    retry_count INTEGER DEFAULT 0,
    metadata TEXT, -- JSON
    FOREIGN KEY (session_id) REFERENCES browser_sessions(id)
);

-- Analytics
CREATE TABLE IF NOT EXISTS account_creation_analytics (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    event_id TEXT UNIQUE NOT NULL,
    timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    account_id TEXT,
    session_id TEXT NOT NULL,
    fingerprint_id TEXT NOT NULL,
    duration_seconds REAL NOT NULL,
    success BOOLEAN NOT NULL,
    error_type TEXT,
    error_message TEXT,
    workflow_steps TEXT NOT NULL, -- JSON
    metadata TEXT, -- JSON
    total_retry_count INTEGER DEFAULT 0,
    network_requests INTEGER DEFAULT 0,
    screenshots_taken INTEGER DEFAULT 0,
    proxy_used BOOLEAN DEFAULT 0,
    proxy_type TEXT,
    browser_type TEXT DEFAULT 'chromium',
    headless BOOLEAN DEFAULT 0,
    success_rate REAL,
    FOREIGN KEY (account_id) REFERENCES accounts(id),
    FOREIGN KEY (session_id) REFERENCES browser_sessions(id),
    FOREIGN KEY (fingerprint_id) REFERENCES browser_fingerprints(id)
);

-- Error Events
CREATE TABLE IF NOT EXISTS error_events (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    error_id TEXT UNIQUE NOT NULL,
    timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    error_type TEXT NOT NULL,
    error_message TEXT NOT NULL,
    stack_trace TEXT,
    context TEXT NOT NULL, -- JSON
    recovery_attempted BOOLEAN DEFAULT 0,
    recovery_successful BOOLEAN DEFAULT 0,
    recovery_attempts TEXT, -- JSON array
    severity TEXT DEFAULT 'medium',
    platform TEXT,
    session_id TEXT,
    account_id TEXT,
    correlation_id TEXT,
    user_impact BOOLEAN DEFAULT 1,
    system_impact BOOLEAN DEFAULT 0,
    data_loss BOOLEAN DEFAULT 0,
    FOREIGN KEY (session_id) REFERENCES browser_sessions(id),
    FOREIGN KEY (account_id) REFERENCES accounts(id)
);

-- Rate Limit Policies
CREATE TABLE IF NOT EXISTS rate_limit_policies (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    action_type TEXT UNIQUE NOT NULL,
    min_delay REAL NOT NULL,
    max_delay REAL NOT NULL,
    adaptive BOOLEAN DEFAULT 1,
    backoff_multiplier REAL DEFAULT 1.5,
    max_retries INTEGER DEFAULT 3,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Session Pool Status
CREATE TABLE IF NOT EXISTS session_pool_status (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    total_sessions INTEGER NOT NULL,
    active_sessions INTEGER NOT NULL,
    healthy_sessions INTEGER NOT NULL,
    failed_sessions INTEGER NOT NULL,
    avg_health_score REAL,
    metadata TEXT -- JSON
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_sessions_last_used ON browser_sessions(last_used);
CREATE INDEX IF NOT EXISTS idx_sessions_health ON browser_sessions(health_score);
CREATE INDEX IF NOT EXISTS idx_sessions_active ON browser_sessions(active);
CREATE INDEX IF NOT EXISTS idx_rate_limits_timestamp ON rate_limit_events(timestamp);
CREATE INDEX IF NOT EXISTS idx_rate_limits_action ON rate_limit_events(action_type);
CREATE INDEX IF NOT EXISTS idx_analytics_timestamp ON account_creation_analytics(timestamp);
CREATE INDEX IF NOT EXISTS idx_analytics_success ON account_creation_analytics(success);
CREATE INDEX IF NOT EXISTS idx_analytics_platform ON account_creation_analytics(metadata);
CREATE INDEX IF NOT EXISTS idx_errors_timestamp ON error_events(timestamp);
CREATE INDEX IF NOT EXISTS idx_errors_type ON error_events(error_type);
CREATE INDEX IF NOT EXISTS idx_errors_severity ON error_events(severity);

-- Views für häufige Abfragen
CREATE VIEW IF NOT EXISTS v_session_health AS
SELECT 
    bs.id,
    bs.health_score,
    bs.error_count,
    bs.success_count,
    bs.last_used,
    COUNT(aca.id) as total_accounts,
    AVG(aca.success_rate) as avg_success_rate
FROM browser_sessions bs
LEFT JOIN account_creation_analytics aca ON bs.id = aca.session_id
GROUP BY bs.id;

CREATE VIEW IF NOT EXISTS v_daily_analytics AS
SELECT 
    DATE(timestamp) as date,
    COUNT(*) as total_attempts,
    SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as successful,
    AVG(duration_seconds) as avg_duration,
    AVG(total_retry_count) as avg_retries
FROM account_creation_analytics
GROUP BY DATE(timestamp);

CREATE VIEW IF NOT EXISTS v_error_summary AS
SELECT 
    error_type,
    COUNT(*) as error_count,
    MIN(timestamp) as first_occurrence,
    MAX(timestamp) as last_occurrence,
    AVG(CASE WHEN recovery_successful = 1 THEN 1.0 ELSE 0.0 END) as recovery_rate
FROM error_events
GROUP BY error_type;