<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LiveComponent Caching Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            padding: 2rem;
            background: #f5f5f5;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            margin-bottom: 2rem;
            color: #333;
        }

        .demo-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 2rem;
            margin-bottom: 2rem;
        }

        .card {
            background: white;
            padding: 1.5rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .card h2 {
            margin-bottom: 1rem;
            color: #444;
            font-size: 1.25rem;
        }

        .single-column {
            grid-column: 1 / -1;
        }

        .performance-meter {
            background: #f9f9f9;
            padding: 1rem;
            border-radius: 4px;
            margin-top: 1rem;
        }

        .performance-bar {
            height: 20px;
            background: linear-gradient(to right, #4CAF50, #FF9800, #f44336);
            border-radius: 10px;
            position: relative;
            margin: 1rem 0;
        }

        .performance-indicator {
            position: absolute;
            top: -5px;
            width: 2px;
            height: 30px;
            background: #333;
            transition: left 0.3s ease;
        }

        .feature-list {
            line-height: 1.8;
            color: #666;
        }

        .feature-list li {
            margin-bottom: 0.5rem;
        }

        .cache-info {
            background: #e3f2fd;
            padding: 1rem;
            border-radius: 4px;
            border-left: 4px solid #2196F3;
        }

        .cache-info p {
            margin: 0.5rem 0;
        }
    </style>
</head>
<body data-module="livecomponent">
    <div class="container">
        <h1>⚡ LiveComponent Caching Demo</h1>

        <div class="demo-grid">
            <div class="card">
                <h2>Stats Component (Cached)</h2>
                {{stats1}}
                <div class="cache-info" style="margin-top: 1rem;">
                    <p><strong>Cache TTL:</strong> 30 seconds</p>
                    <p><strong>Tag:</strong> stats, dashboard-components</p>
                    <p><strong>Performance:</strong> ~500ms without cache, ~1ms with cache</p>
                </div>
            </div>

            <div class="card">
                <h2>Stats Component (Uncached)</h2>
                {{stats2}}
                <div class="cache-info" style="margin-top: 1rem; background: #ffebee; border-color: #f44336;">
                    <p><strong>Cache:</strong> Disabled</p>
                    <p><strong>Performance:</strong> Always ~500ms (expensive computation)</p>
                </div>
            </div>

            <div class="card single-column">
                <h2>How Component Caching Works</h2>
                <ul class="feature-list">
                    <li>✅ <strong>Automatic Caching:</strong> Components implementing `Cacheable` are cached automatically</li>
                    <li>✅ <strong>TTL Support:</strong> Configure cache duration per component (default: 30 seconds)</li>
                    <li>✅ <strong>Cache Keys:</strong> Unique keys based on component ID and state</li>
                    <li>✅ <strong>Cache Tags:</strong> Group components for batch invalidation</li>
                    <li>✅ <strong>Conditional Caching:</strong> `shouldCache()` controls caching per request</li>
                    <li>✅ <strong>Manual Invalidation:</strong> Refresh action invalidates cache on demand</li>
                    <li>✅ <strong>Performance Boost:</strong> 500x faster rendering with cache</li>
                </ul>
            </div>

            <div class="card single-column">
                <h2>Implementation Example</h2>
                <pre style="background: #f5f5f5; padding: 1rem; border-radius: 4px; overflow-x: auto; font-size: 0.875rem;"><code>#[LiveComponent('stats')]
final readonly class StatsComponent implements Cacheable
{
    public function getCacheKey(): string
    {
        return $this->id . ':' . ($this->cacheEnabled ? 'enabled' : 'disabled');
    }

    public function getCacheTTL(): Duration
    {
        return Duration::fromSeconds(30);
    }

    public function shouldCache(): bool
    {
        return $this->initialData['cache_enabled'] ?? true;
    }

    public function getCacheTags(): array
    {
        return ['stats', 'dashboard-components'];
    }
}</code></pre>
            </div>

            <div class="card single-column">
                <h2>Cache Invalidation Strategies</h2>
                <ul class="feature-list">
                    <li><strong>Time-based:</strong> Cache expires after TTL (30 seconds)</li>
                    <li><strong>Manual:</strong> Refresh button invalidates cache immediately</li>
                    <li><strong>Tag-based:</strong> Invalidate all components with tag "stats"</li>
                    <li><strong>Action-based:</strong> Cache invalidated after state-changing actions</li>
                </ul>
            </div>
        </div>

        <div class="card" style="background: #fff3cd; border: 1px solid #ffc107;">
            <h2>💡 Performance Tip</h2>
            <p style="margin-bottom: 1rem;">Notice the "Render Count" difference:</p>
            <ul class="feature-list">
                <li>Cached component: Render count stays at 1 (served from cache)</li>
                <li>Uncached component: Render count increases on every refresh</li>
                <li>Click "Refresh" to see cache invalidation in action</li>
                <li>Toggle cache on/off to compare performance</li>
            </ul>
        </div>
    </div>

    <script>
        // Performance monitoring (Demo-specific)
        document.addEventListener('livecomponent:stats:refreshed', (e) => {
            console.log('Stats refreshed:', e.detail);
        });

        const startTime = performance.now();
        window.addEventListener('load', () => {
            const loadTime = performance.now() - startTime;
            console.log(`Page load time: ${loadTime.toFixed(2)}ms`);
        });
    </script>
</body>
</html>
