<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auto-Documentation Dashboard</title>
    <style>
        :root {
            --primary-color: #4a6cf7;
            --secondary-color: #6e42c1;
            --success-color: #28a745;
            --danger-color: #dc3545;
            --warning-color: #ffc107;
            --light-color: #f8f9fa;
            --dark-color: #343a40;
            --body-color: #1d0d2b;
            --text-color: #333;
            --border-color: #ddd;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--body-color);
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            background-color: #1d0d2b;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px 0;
            margin-bottom: 30px;
        }
        
        .header-content {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .logo {
            font-size: 24px;
            font-weight: bold;
            color: var(--primary-color);
        }
        
        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 25px;
            overflow: hidden;
        }
        
        .card-header {
            padding: 15px 20px;
            border-bottom: 1px solid var(--border-color);
            background-color: #f9fafb;
        }
        
        .card-title {
            margin: 0;
            font-size: 18px;
            font-weight: 600;
        }
        
        .card-body {
            padding: 20px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
        }
        
        input[type="text"],
        input[type="number"],
        select {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-size: 16px;
            transition: border-color 0.2s;
        }
        
        input[type="text"]:focus,
        input[type="number"]:focus,
        select:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.2);
        }
        
        .checkbox-group {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .checkbox-group input[type="checkbox"] {
            margin-right: 10px;
        }
        
        .checkbox-group label {
            margin-bottom: 0;
            font-weight: normal;
        }
        
        button {
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 4px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        
        button:hover {
            background-color: #3551d3;
        }
        
        button.secondary {
            background-color: white;
            color: var(--primary-color);
            border: 1px solid var(--primary-color);
        }
        
        button.secondary:hover {
            background-color: #f8f9fa;
        }
        
        button.danger {
            background-color: var(--danger-color);
        }
        
        button.danger:hover {
            background-color: #c82333;
        }
        
        .project-card {
            border: 1px solid var(--border-color);
            border-radius: 6px;
            padding: 15px;
            margin-bottom: 15px;
            background-color: white;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        
        .project-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .project-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }
        
        .project-title {
            font-size: 18px;
            font-weight: 600;
            margin: 0;
        }
        
        .project-status {
            font-size: 14px;
            padding: 3px 10px;
            border-radius: 20px;
            background-color: #e9ecef;
        }
        
        .status-watching {
            background-color: rgba(40, 167, 69, 0.2);
            color: var(--success-color);
        }
        
        .status-static {
            background-color: rgba(108, 117, 125, 0.2);
            color: #6c757d;
        }
        
        .project-info {
            margin-bottom: 15px;
            color: #666;
        }
        
        .project-info p {
            margin: 5px 0;
        }
        
        .project-actions {
            display: flex;
            gap: 10px;
        }
        
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: var(--primary-color);
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #6c757d;
            font-size: 14px;
            text-transform: uppercase;
        }
        
        .loader {
            border: 3px solid #f3f3f3;
            border-radius: 50%;
            border-top: 3px solid var(--primary-color);
            width: 24px;
            height: 24px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin-right: 10px;
            vertical-align: middle;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .alert {
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            border-left: 4px solid transparent;
        }
        
        .alert-success {
            background-color: rgba(40, 167, 69, 0.1);
            border-left-color: var(--success-color);
            color: #155724;
        }
        
        .alert-danger {
            background-color: rgba(220, 53, 69, 0.1);
            border-left-color: var(--danger-color);
            color: #721c24;
        }
        
        .alert-warning {
            background-color: rgba(255, 193, 7, 0.1);
            border-left-color: var(--warning-color);
            color: #856404;
        }
        
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <header>
        <div class="container header-content">
            <div class="logo">FastAPI Auto-Doc</div>
        </div>
    </header>

    <div class="container">
        <div id="alerts-container"></div>
        
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-value" id="total-projects">0</div>
                <div class="stat-label">Projects</div>
            </div>
            <div class="stat-card">
                <div class="stat-value" id="total-files">0</div>
                <div class="stat-label">Files</div>
            </div>
            <div class="stat-card">
                <div class="stat-value" id="total-functions">0</div>
                <div class="stat-label">Functions</div>
            </div>
            <div class="stat-card">
                <div class="stat-value" id="total-classes">0</div>
                <div class="stat-label">Classes</div>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">Document a Project</h2>
            </div>
            <div class="card-body">
                <form id="document-form">
                    <div class="form-group">
                        <label for="project_path">Project Path:</label>
                        <input type="text" id="project_path" name="project_path" placeholder="/project/path" required>
                    </div>
                    
                    <div class="checkbox-group">
                        <input type="checkbox" id="watch_mode" name="watch_mode">
                        <label for="watch_mode">Watch for changes</label>
                    </div>
                    
                    <div class="checkbox-group">
                        <input type="checkbox" id="include_private" name="include_private">
                        <label for="include_private">Include private members (starting with _)</label>
                    </div>
                    
                    <div class="form-group">
                        <label for="excluded_dirs">Excluded Directories (comma-separated):</label>
                        <input type="text" id="excluded_dirs" name="excluded_dirs" value="__pycache__,.git,.venv,venv,env,node_modules">
                    </div>
                    
                    <div class="form-group">
                        <label for="excluded_files">Excluded Files (comma-separated):</label>
                        <input type="text" id="excluded_files" name="excluded_files">
                    </div>
                    
                    <div class="form-group">
                        <label for="doc_output_path">Documentation Output Directory:</label>
                        <input type="text" id="doc_output_path" name="doc_output_path" value="docs">
                    </div>
                    
                    <button type="submit" id="submit-btn">
                        <span id="submit-spinner" class="loader hidden"></span>
                        Generate Documentation
                    </button>
                </form>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">Documented Projects</h2>
            </div>
            <div class="card-body">
                <div id="projects-container">
                    <p>Loading projects...</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        
        function showAlert(message, type = 'success') {
            const alertsContainer = document.getElementById('alerts-container');
            const alertDiv = document.createElement('div');
            alertDiv.className = `alert alert-${type}`;
            alertDiv.textContent = message;
            
            
            const closeButton = document.createElement('button');
            closeButton.innerHTML = '&times;';
            closeButton.style.float = 'right';
            closeButton.style.background = 'none';
            closeButton.style.border = 'none';
            closeButton.style.color = 'inherit';
            closeButton.style.fontSize = '20px';
            closeButton.style.cursor = 'pointer';
            closeButton.style.padding = '0 5px';
            closeButton.onclick = function() {
                alertsContainer.removeChild(alertDiv);
            };
            alertDiv.prepend(closeButton);
            
            alertsContainer.appendChild(alertDiv);
            
            
            setTimeout(() => {
                if (alertDiv.parentNode) {
                    alertsContainer.removeChild(alertDiv);
                }
            }, 5000);
        }
        
        
        function formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleString();
        }
        
        
        async function loadProjects() {
            try {
                const response = await fetch('/api/v1/documentation/projects');
                
                if (!response.ok) {
                    throw new Error('Failed to load projects');
                }
                
                const data = await response.json();
                const projectsContainer = document.getElementById('projects-container');
                
                if (data.projects.length === 0) {
                    projectsContainer.innerHTML = '<p>No projects documented yet.</p>';
                    return;
                }
                
                let projectsHTML = '';
                let totalFiles = 0;
                let totalFunctions = 0;
                let totalClasses = 0;
                
                data.projects.forEach(project => {
                    const isWatching = project.watching;
                    const statusClass = isWatching ? 'status-watching' : 'status-static';
                    const statusText = isWatching ? 'Watching' : 'Static';
                    
                    totalFiles += project.stats.files || 0;
                    totalFunctions += project.stats.functions || 0;
                    totalClasses += project.stats.classes || 0;
                    
                    projectsHTML += `
                        <div class="project-card" data-project="${project.path}">
                            <div class="project-header">
                                <h3 class="project-title">${project.name}</h3>
                                <span class="project-status ${statusClass}">${statusText}</span>
                            </div>
                            <div class="project-info">
                                <p>Path: ${project.path}</p>
                                <p>Last updated: ${formatDate(project.last_updated)}</p>
                                <p>Files: ${project.stats.files} | Functions: ${project.stats.functions} | Classes: ${project.stats.classes}</p>
                            </div>
                            <div class="project-actions">
                                <a href="http://127.0.0.1:8000/docs/index.html" target="_blank">
                                    <button>View Documentation</button>
                                </a>
                                ${isWatching ? 
                                    `<button class="danger" onclick="stopWatching('${project.path}')">Stop Watching</button>` : 
                                    `<button class="secondary" onclick="startWatching('${project.path}')">Start Watching</button>`
                                }
                                <button class="secondary" onclick="regenerateDocumentation('${project.path}')">Regenerate</button>
                            </div>
                        </div>
                    `;
                });
                
                projectsContainer.innerHTML = projectsHTML;
                
                
                document.getElementById('total-projects').textContent = data.projects.length;
                document.getElementById('total-files').textContent = totalFiles;
                document.getElementById('total-functions').textContent = totalFunctions;
                document.getElementById('total-classes').textContent = totalClasses;
                
            } catch (error) {
                console.error('Error loading projects:', error);
                showAlert(`Error loading projects: ${error.message}`, 'danger');
            }
        }
        
        
        async function stopWatching(projectPath) {
            try {
                const response = await fetch(`/api/v1/documentation/watch/${encodeURIComponent(projectPath)}`, {
                    method: 'DELETE',
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.detail || 'Failed to stop watching');
                }
                
                showAlert(`Stopped watching project: ${projectPath}`);
                loadProjects();
                
            } catch (error) {
                console.error('Error stopping watch:', error);
                showAlert(`Error: ${error.message}`, 'danger');
            }
        }
        
        async function startWatching(projectPath) {
            try {
                const response = await fetch(`/api/v1/documentation/watch/${encodeURIComponent(projectPath)}`, {
                    method: 'POST',
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.detail || 'Failed to start watching');
                }
                
                showAlert(`Started watching project: ${projectPath}`);
                loadProjects();
                
            } catch (error) {
                console.error('Error starting watch:', error);
                showAlert(`Error: ${error.message}`, 'danger');
            }
        }
        
        
        async function regenerateDocumentation(projectPath) {
            try {
                const response = await fetch(`/api/v1/documentation/refresh/${encodeURIComponent(projectPath)}`, {
                    method: 'POST',
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.detail || 'Failed to regenerate documentation');
                }
                
                const result = await response.json();
                showAlert(`Documentation regenerated successfully for: ${projectPath}`);
                loadProjects();
                
            } catch (error) {
                console.error('Error regenerating documentation:', error);
                showAlert(`Error: ${error.message}`, 'danger');
            }
        }
        
        
        document.getElementById('document-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const submitBtn = document.getElementById('submit-btn');
            const spinner = document.getElementById('submit-spinner');
            
            
            submitBtn.disabled = true;
            spinner.classList.remove('hidden');
            
            const projectPath = document.getElementById('project_path').value;
            const watchMode = document.getElementById('watch_mode').checked;
            const includePrivate = document.getElementById('include_private').checked;
            const excludedDirs = document.getElementById('excluded_dirs').value
                .split(',').map(d => d.trim()).filter(d => d);
            const excludedFiles = document.getElementById('excluded_files').value
                .split(',').map(f => f.trim()).filter(f => f);
            const docOutputPath = document.getElementById('doc_output_path').value;
            
            try {
                const response = await fetch('/api/v1/documentation/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        project_path: projectPath,
                        watch_mode: watchMode,
                        include_private: includePrivate,
                        excluded_dirs: excludedDirs,
                        excluded_files: excludedFiles,
                        doc_output_path: docOutputPath
                    }),
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.detail || 'Failed to document project');
                }
                
                const result = await response.json();
                showAlert(`Documentation generated successfully! Found ${result.total_files} files, ${result.total_functions} functions, and ${result.total_classes} classes.`);
                
                
                document.getElementById('document-form').reset();
                document.getElementById('excluded_dirs').value = "__pycache__,.git,.venv,venv,env,node_modules";
                document.getElementById('doc_output_path').value = "docs";
                
                
                loadProjects();
                
            } catch (error) {
                console.error('Error documenting project:', error);
                showAlert(`Error: ${error.message}`, 'danger');
            } finally {
                
                submitBtn.disabled = false;
                spinner.classList.add('hidden');
            }
        });
        
        document.addEventListener('DOMContentLoaded', loadProjects);
    </script>
</body>
</html>