<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Puzzle - Your Creative Writing Hub</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
        <link rel="stylesheet" href="/static/css/style.css">
    </head>
<body class="bg-light">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="/">Puzzle</a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="/home">Home</a>
                <a class="nav-link active" href="/publish">Publish</a>
                <a class="nav-link" href="/collaborations">Collaborations</a>
                <a class="nav-link" href="/profile">Profile</a>
                <a class="nav-link" href="/logout">Logout</a>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <h2>Publish New Article</h2>
        <form id="publishForm">
            <div class="mb-3">
                <label for="title" class="form-label">Title</label>
                <input type="text" class="form-control" id="title" name="title" required>
            </div>
            <div class="mb-3">
                <label for="content" class="form-label">Content</label>
                <textarea class="form-control" id="content" name="content" rows="10" required></textarea>
            </div>
            <div class="mb-3">
                <label for="collaborator" class="form-label">Collaborator (Optional)</label>
                <input type="text" class="form-control" id="collaborator" name="collaborator" placeholder="Enter username to collaborate">
            </div>
            <div id="message"></div>
            <button type="submit" class="btn btn-primary">Publish</button>
        </form>
    </div>

    <script>
    document.getElementById('publishForm').addEventListener('submit', async function(e) {
        e.preventDefault();
        const formData = new FormData(this);
        try {
            const response = await fetch('/publish', {
                method: 'POST',
                body: formData
            });
            const result = await response.json();
            if (response.ok) {
                window.location.href = '/home';
            } else {
                document.getElementById('message').innerHTML = 
                    `<div class="alert alert-danger">${result.error}</div>`;
            }
        } catch (err) {
            document.getElementById('message').innerHTML = 
                `<div class="alert alert-danger">Error publishing article</div>`;
        }
    });
    </script>
</body>
</html>