# 宇之然内容创作平台 - 文章预览API

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import Dict

from app.database import get_db
from app.models import Topic

router = APIRouter()

@router.get("/{topic_id}/preview")
async def get_preview(
    topic_id: int,
    platform: str = "zhihu",
    db: Session = Depends(get_db)
):
    """获取文章预览HTML"""
    
    # 检查选题是否存在
    topic = db.query(Topic).filter(Topic.id == topic_id).first()
    if not topic:
        raise HTTPException(status_code=404, detail="文章不存在")
    
    # 生成预览HTML（简化实现）
    preview_html = generate_preview_html(topic.title, platform)
    
    return {"html": preview_html}

def generate_preview_html(title: str, platform: str) -> str:
    """根据平台和标题生成预览HTML"""
    
    base_template = f"""
    <div class="article-preview">
        <header class="header">
            <h1>{title}</h1>
            <div class="meta">
                <span class="platform">{platform}</span>
                <span class="date">2026-04-26</span>
            </div>
        </header>
        
        <div class="content">
            <p>这里是文章的正文内容...</p>
            <p>文章包含多个段落，展示不同的写作风格和结构。</p>
            <p>在实际生产环境中，这里应该是完整的文章内容。</p>
        </div>
        
        <footer class="footer">
            <div class="tags">
                <span class="tag">#人工智能</span>
                <span class="tag">#科技</span>
                <span class="tag">#趋势</span>
            </div>
            <div class="interaction">
                <button class="like">👍 点赞</button>
                <button class="share">🔗 分享</button>
            </div>
        </footer>
    </div>
    """
    
    return base_template