<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Database Schema - Generated on {{ timestamp }}</title>
    <script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .header {
            background: #2c3e50;
            color: white;
            padding: 20px;
            text-align: center;
        }
        .header h1 {
            margin: 0;
            font-size: 2em;
        }
        .timestamp {
            opacity: 0.7;
            font-size: 0.9em;
            margin-top: 5px;
        }
        .diagram-container {
            padding: 30px;
            text-align: center;
            background: white;
        }
        .controls {
            margin-bottom: 20px;
            text-align: center;
        }
        button {
            background: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 5px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s;
        }
        button:hover {
            background: #2980b9;
        }
        .mermaid {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .footer {
            background: #ecf0f1;
            padding: 15px;
            text-align: center;
            color: #7f8c8d;
            font-size: 0.9em;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🗄️ Database Schema Diagram</h1>
            <div class="timestamp">Generated on {{ formatted_timestamp }}</div>
        </div>
        
        <div class="diagram-container">
            <div class="controls">
                <button onclick="downloadSVG()">📥 Download image</button>
                <button onclick="copyMermaidCode()">📋 Copy Mermaid Code</button>
            </div>
            
            <div class="mermaid" id="diagram">
{{ mermaid_code }}
            </div>
        </div>
        
        <div class="footer">
            Generated by Framefox Framework • 
            <a href="https://mermaid.js.org/" target="_blank">Powered by Mermaid.js</a>
        </div>
    </div>

    <script>
        // Mermaid configuration
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#3498db',
                primaryTextColor: '#2c3e50',
                primaryBorderColor: '#2980b9',
                lineColor: '#34495e'
            }
        });

        let currentTheme = 'default';
        const mermaidCode = `{{ mermaid_code }}`;

        function downloadSVG() {
            const svg = document.querySelector('#diagram svg');
            if (svg) {
                const svgData = new XMLSerializer().serializeToString(svg);
                const svgBlob = new Blob([svgData], {type: 'image/svg+xml;charset=utf-8'});
                const svgUrl = URL.createObjectURL(svgBlob);
                const downloadLink = document.createElement('a');
                downloadLink.href = svgUrl;
                downloadLink.download = 'database_schema.svg';
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);
            }
        }

        function copyMermaidCode() {
            navigator.clipboard.writeText(mermaidCode).then(function() {
                alert('Mermaid code copied to clipboard!');
            }, function(err) {
                console.error('Could not copy text: ', err);
                // Fallback
                const textArea = document.createElement('textarea');
                textArea.value = mermaidCode;
                document.body.appendChild(textArea);
                textArea.select();
                document.execCommand('copy');
                document.body.removeChild(textArea);
                alert('Mermaid code copied to clipboard!');
            });
        }
    </script>
</body>
</html>