<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Idea Generator | AI Assistant</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">
    <script src="https://kit.fontawesome.com/475afa2c37.js" crossorigin="anonymous"></script>
    <style>
        .ideas-section {
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .input-group {
            display: flex;
            gap: 1rem;
            margin-bottom: 2rem;
        }

        .input-field {
            flex-grow: 1;
            padding: 0.75rem 1rem;
            border: 1px solid var(--text-secondary);
            border-radius: 0.5rem;
            background-color: var(--background-color);
            color: var(--text-primary);
            font-family: 'Poppins', sans-serif;
        }

        .button {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 0.5rem;
            background-color: var(--accent-color);
            color: white;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .button:hover {
            background-color: var(--primary-color);
            transform: translateY(-1px);
        }

        .loading-section {
            display: none;
            text-align: center;
            padding: 2rem;
            background-color: var(--surface-color);
            border-radius: 1rem;
            margin-bottom: 2rem;
        }

        .response-section {
            background-color: var(--surface-color);
            border-radius: 1rem;
            padding: 2rem;
            margin-bottom: 2rem;
            display: none;
        }

        .response-content {
            white-space: pre-wrap;
            color: var(--text-primary);
            line-height: 1.6;
            margin-bottom: 1rem;
        }

        .action-buttons {
            display: flex;
            gap: 1rem;
            justify-content: flex-end;
        }

        .home-button {
            position: fixed;
            bottom: 2rem;
            right: 2rem;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: var(--accent-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .home-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        }
    </style>
</head>
<body>
    <nav class="top-nav">
        <div class="logo">
            <span class="logo-text">AI Assistant</span>
        </div>
        <div class="user-profile">
            <button class="theme-toggle" aria-label="Toggle theme">
                <i class="fas fa-moon"></i>
            </button>
        </div>
    </nav>

    <main class="ideas-section">
        <h1>Idea Generator</h1>
        
        <div class="input-group">
            <input type="text" 
                   id="topicInput" 
                   class="input-field" 
                   placeholder="Enter your topic for ideas...">
            <button class="button" id="generateIdeas">Generate Ideas</button>
        </div>

        <div class="loading-section" id="loadingSection">
            <img src="images/loading.gif" alt="Loading..." width="50" height="50">
            <p>Generating ideas...</p>
        </div>

        <div class="response-section" id="responseSection">
            <div class="response-content" id="ideasContent"></div>
            <div class="action-buttons">
                <button class="button" id="emailIdeas">Email Ideas</button>
            </div>
        </div>
    </main>

    <a href="index.php" class="home-button">
        <i class="fas fa-house"></i>
    </a>

    <script>
        document.getElementById('generateIdeas').addEventListener('click', generateIdeas);
        document.getElementById('emailIdeas').addEventListener('click', emailIdeas);

        async function generateIdeas() {
            const topic = document.getElementById('topicInput').value;
            if (!topic) {
                alert('Please enter a topic');
                return;
            }

            const loadingSection = document.getElementById('loadingSection');
            const responseSection = document.getElementById('responseSection');
            const ideasContent = document.getElementById('ideasContent');

            loadingSection.style.display = 'block';
            responseSection.style.display = 'none';

            const prompt = `Generate creative and innovative ideas about the following topic: ${topic}. No idea is out of bounds. Don't judge any ideas, just list them in HTML format.`;

            try {
                const response = await fetch('ai_endpointIdeas.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ prompt: prompt })
                });

                const data = await response.json();
                ideasContent.innerHTML = data.response;
                loadingSection.style.display = 'none';
                responseSection.style.display = 'block';
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred while generating ideas');
                loadingSection.style.display = 'none';
            }
        }

        async function emailIdeas() {
            const ideas = document.getElementById('ideasContent').textContent;
            const topic = document.getElementById('topicInput').value;

            try {
                const response = await fetch('email_endpoint.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        subject: `Ideas Generated for: ${topic}`,
                        content: ideas
                    })
                });

                const data = await response.json();
                if (data.success) {
                    alert('Ideas have been emailed to you');
                } else {
                    alert('Failed to send email');
                }
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred while sending the email');
            }
        }
    </script>
    <script src="scripts.js"></script>
</body>
</html>
