<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speech to Text Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: white;
            border-radius: 4px;
            display: none;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        #uploadForm {
            margin-bottom: 20px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .topic {
            background-color: #f9f9f9;
            padding: 15px;
            margin: 10px 0;
            border-radius: 4px;
            border-left: 4px solid #4CAF50;
        }
        
        .topic h4 {
            margin: 0 0 10px 0;
            color: #333;
        }

        .section-divider {
            margin: 30px 0;
            border-top: 2px solid #eee;
        }

        .analysis-section {
            margin: 20px 0;
        }

        .analysis-header {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .transcript-section {
            background-color: #ffffff;
            padding: 20px;
            margin: 15px 0;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .transcript-section h4 {
            color: #2c3e50;
            margin: 0 0 15px 0;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }

        .transcript-content {
            margin-bottom: 15px;
            line-height: 1.6;
        }

        .transcript-summary {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            font-style: italic;
            color: #666;
        }

        .section-label {
            font-size: 0.9em;
            color: #666;
            text-transform: uppercase;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Speech to Text Converter</h1>
        <form id="uploadForm">
            <input type="file" id="fileInput" accept=".wav,.mp3,.mp4,.avi,.mov,.m4a" required>
            <button type="submit">Process File</button>
        </form>
        
        <div class="loading" id="loading">
            Processing... Please wait...
        </div>

        <div class="result" id="result">
            <div class="analysis-section">
                <h3 class="analysis-header">Lecture Content:</h3>
                <div id="lectureSections"></div>
            </div>

            <div class="section-divider"></div>
            
            <div class="analysis-section">
                <h3 class="analysis-header">Lecture Summary:</h3>
                <p id="lectureSummary"></p>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const fileInput = document.getElementById('fileInput');
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            const lectureSections = document.getElementById('lectureSections');
            const lectureSummary = document.getElementById('lectureSummary');
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            
            try {
                loading.style.display = 'block';
                result.style.display = 'none';
                
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    try {
                        const groqData = typeof data.groq_response === 'string' 
                            ? JSON.parse(data.groq_response) 
                            : data.groq_response;

                        // Display lecture sections
                        if (groqData && groqData.lecture_sections) {
                            lectureSections.innerHTML = groqData.lecture_sections
                                .map((section, index) => `
                                    <div class="transcript-section">
                                        <h4>${index + 1}. ${section.topic}</h4>
                                        <div class="section-label">Lecture Content:</div>
                                        <div class="transcript-content">${section.transcript}</div>
                                        <div class="section-label">Explanation:</div>
                                        <div class="transcript-summary">${section.explanation}</div>
                                    </div>
                                `).join('');
                        }
                        
                        // Display lecture summary
                        if (groqData && groqData.lecture_summary) {
                            lectureSummary.textContent = groqData.lecture_summary;
                        }
                        
                        result.style.display = 'block';
                    } catch (parseError) {
                        console.error('Error parsing response:', parseError);
                        // Fallback display
                        lectureSections.innerHTML = `
                            <div class="transcript-section">
                                <h4>Complete Lecture Transcription</h4>
                                <div class="transcript-content">${data.transcription}</div>
                            </div>`;
                        result.style.display = 'block';
                    }
                } else {
                    throw new Error(data.error || 'Failed to process file');
                }
            } catch (error) {
                console.error('Error:', error);
                alert(error.message || 'An error occurred while processing the file');
            } finally {
                loading.style.display = 'none';
            }
        });
    </script>
</body>
</html>
