---
/**
 * Preview Post/Article Route
 * Shows a single generated article in preview mode
 */

import { getDirectusClient, readItem } from '@/lib/directus/client';
import type { GeneratedArticle } from '@/types/schema';

const { postId } = Astro.params;

if (!postId) {
    return Astro.redirect('/admin/seo/articles');
}

let article: GeneratedArticle | null = null;
let error: string | null = null;

try {
    const client = getDirectusClient();
    const result = await client.request(readItem('generated_articles', postId));
    article = result as GeneratedArticle;
} catch (err) {
    error = err instanceof Error ? err.message : 'Failed to load article';
    console.error('Preview error:', err);
}

if (!article) {
    return Astro.redirect('/admin/seo/articles');
}
---

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Preview: {article.title}</title>
    {article.meta_desc && <meta name="description" content={article.meta_desc}>}
    <style>
        .preview-banner {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 20px;
            z-index: 9999;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .preview-content {
            margin-top: 60px;
            padding: 40px 20px;
            max-width: 800px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .preview-badge {
            background: rgba(255,255,255,0.2);
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .close-preview, .share-preview {
            background: rgba(255,255,255,0.2);
            border: none;
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
            transition: background 0.2s;
        }
        
        .close-preview:hover, .share-preview:hover {
            background: rgba(255,255,255,0.3);
        }
        
        article {
            font-family: Georgia, serif;
            line-height: 1.8;
            color: #333;
        }
        
        article h1 {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
            font-weight: 700;
            line-height: 1.2;
        }
        
        article p {
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <!-- Preview Mode Banner -->
    <div class="preview-banner">
        <div style="display: flex; align-items: center; gap: 12px;">
            <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
            </svg>
            <span style="font-weight: 600; font-size: 14px;">PREVIEW MODE</span>
            <span class="preview-badge">{article.is_published ? 'Published' : 'Draft'}</span>
        </div>
        <div style="display: flex; gap: 12px; align-items: center;">
            <button class="share-preview" onclick="navigator.clipboard.writeText(window.location.href); alert('Preview URL copied!')">
                📋 Copy Link
            </button>
            <button class="close-preview" onclick="window.close()">Close Preview</button>
        </div>
    </div>

    <!-- Article Content -->
    <div class="preview-content">
        {error ? (
            <div style="background: #fee; border: 1px solid #fcc; color: #c00; padding: 20px; border-radius: 8px;">
                <h2>Error Loading Preview</h2>
                <p>{error}</p>
            </div>
        ) : (
            <article>
                <h1>{article.title}</h1>
                
                {article.meta_desc && (
                    <p style="color: #666; font-size: 1.1rem; font-style: italic; margin-bottom: 2rem;">
                        {article.meta_desc}
                    </p>
                )}
                
                {article.html_content && (
                    <div>
                        <Fragment set:html={article.html_content} />
                    </div>
                )}
                
                {!article.html_content && (
                    <p style="color: #999; font-style: italic;">No content generated yet</p>
                )}
            </article>
        )}
    </div>
</body>
</html>
