"use client";

import type { OptimizedArticle } from "../lib/domain/types";

interface OptimizedPreviewProps {
  article: OptimizedArticle | null;
  jobId: string | null;
}

const exportFiles = ["optimized.md", "optimized.docx", "qa_report.json"];

export function OptimizedPreview({
  article,
  jobId,
}: OptimizedPreviewProps) {
  if (!article) {
    return (
      <section className="panel empty-panel">
        <div className="panel-heading">优化结果</div>
      </section>
    );
  }

  return (
    <section className="panel stack">
      <div className="panel-heading">优化结果</div>
      <article className="preview">
        <h2>{article.title}</h2>
        <p>{article.summary}</p>
        <pre>{article.body_markdown}</pre>
      </article>
      <div>
        <h3>图片建议</h3>
        <ul>
          {article.image_suggestions.map((item) => (
            <li key={item.source}>
              <strong>{item.source}</strong>: {item.suggestion}
            </li>
          ))}
        </ul>
      </div>
      <div>
        <h3>修改位置</h3>
        <div className="tag-row">
          {article.changed_sections.map((section) => (
            <span className="tag" key={section}>
              {section}
            </span>
          ))}
        </div>
      </div>
      {article.requires_user_confirmation.length > 0 && (
        <div>
          <h3>需要人工确认</h3>
          <ul>
            {article.requires_user_confirmation.map((item) => (
              <li key={item}>{item}</li>
            ))}
          </ul>
        </div>
      )}
      <div className="export-row">
        {exportFiles.map((fileName) =>
          jobId ? (
            <a
              className="download-link"
              href={`/api/jobs/${jobId}/exports/${fileName}`}
              key={fileName}
            >
              {fileName}
            </a>
          ) : (
            <button disabled key={fileName} type="button">
              {fileName}
            </button>
          ),
        )}
      </div>
    </section>
  );
}
