<!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" 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">
        <div class="card">
            <div class="card-body">
                <h1 class="card-title">{{ article.title }}</h1>
                <div class="text-muted mb-4">
                    <span class="d-none author-uuid">{{ article.author_uuid }}</span>
                    {% if article.collaborator_uuid %}
                    <span class="d-none collaborator-uuid">{{ article.collaborator_uuid }}</span>
                    {% endif %}
                    <!-- Author and collaborator info -->
                    <small>
                        Posted by {{ article.author_name }}
                        {% if article.collaborator_name %}
                        in collaboration with {{ article.collaborator_name }}
                        {% endif %}
                        on {{ article.created_at }}
                    </small>
                </div>
                <div class="card-text">
                    {{ article.content }}
                </div>
            </div>
        </div>

        {% if session.uuid == article.author_uuid %}
        <div class="mt-3">
            <a href="/article/{{ article.uuid }}/edit" class="btn btn-primary">Edit Article</a>
            <button class="btn btn-danger" onclick="deleteArticle('{{ article.uuid }}')">Delete Article</button>
        </div>
        {% endif %}
    </div>

    <script>
        function deleteArticle(uuid) {
            if (confirm('Are you sure you want to delete this article?')) {
                fetch(`/article/${uuid}`, {
                    method: 'DELETE',
                }).then(response => {
                    if (response.ok) {
                        window.location.href = '/home';
                    } else {
                        alert('Failed to delete article');
                    }
                });
            }
        }
    </script>
</body>
</html>