import React, { useState, useEffect, useRef } from 'react'; import { FileText, Plus, History, Settings, User, ChevronRight, Sparkles, Code, MessageSquare, Briefcase, Download, Copy, Check, AlertCircle, ArrowLeft, Trash2, Cloud, Sun, Moon } from 'lucide-react'; // Firebase Imports import { initializeApp } from 'firebase/app'; import { getAuth, signInWithCustomToken, signInAnonymously, onAuthStateChanged } from 'firebase/auth'; import { getFirestore, collection, doc, setDoc, onSnapshot, deleteDoc } from 'firebase/firestore'; // --- UTILITIES & API --- const apiKey = ""; // Provided by execution environment // Firebase Setup const firebaseConfig = typeof __firebase_config !== 'undefined' ? JSON.parse(__firebase_config) : {}; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getFirestore(app); const appId = typeof __app_id !== 'undefined' ? __app_id : 'default-app-id'; // Exponential backoff fetcher for Gemini API const fetchWithRetry = async (url, options, maxRetries = 5) => { let delays = [1000, 2000, 4000, 8000, 16000]; for (let i = 0; i < maxRetries; i++) { try { const response = await fetch(url, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { if (i === maxRetries - 1) throw error; await new Promise(res => setTimeout(res, delays[i])); } } }; const generatePRDWithAI = async (formData) => { const systemPrompt = `You are an expert Product Manager and Software Architect. Your job is to transform raw software ideas into structured, engineering-ready Product Requirements Documents (PRDs). Follow these rules: 1. Output ONLY in Markdown format. 2. Be highly specific and technical, avoiding fluff. 3. You MUST include exactly these sections: - 1. Overview (Problem Statement & Core Value) - 2. Target Audience - 3. MVP Feature Scope (Bullet points of core features) - 4. User Flow (Step-by-step) - 5. System Architecture (Mermaid Sequence Diagram using \`\`\`mermaid) - 6. Database Schema (Mermaid ERD using \`\`\`mermaid) - 7. Technical & Design Constraints 4. Write the PRD in the requested language.`; const userPrompt = ` Please generate a PRD based on the following input: - Product Name: ${formData.productName || 'Untitled Project'} - Raw Idea: ${formData.rawIdea} - Target User: ${formData.targetUser || 'General users'} - Platform: ${formData.platform} - Scope Level: ${formData.scopeLevel} - Preferred Language: ${formData.language} - Detail Level: ${formData.detailLevel} `; const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key=${apiKey}`; const payload = { contents: [{ parts: [{ text: userPrompt }] }], systemInstruction: { parts: [{ text: systemPrompt }] } }; const data = await fetchWithRetry(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); return data.candidates?.[0]?.content?.parts?.[0]?.text || "Generation failed. Please try again."; }; // Dynamic Script Loader const loadScript = (src, globalVar) => { return new Promise((resolve, reject) => { if (window[globalVar]) return resolve(window[globalVar]); const script = document.createElement('script'); script.src = src; script.onload = () => resolve(window[globalVar]); script.onerror = reject; document.head.appendChild(script); }); }; // --- GLOBAL STYLES (Roboto Font) --- const GlobalStyles = () => (