import type { MindSpacePublication, PlazaPostBrief } from '../types';
import {
  resolvePlazaHomeUrl,
  resolvePlazaPostUrl,
  resolvePublicPageUrl,
} from '../utils/publicUrl';

export function MindSpacePublishSuccess({
  pageTitle,
  publication,
  plazaPost,
  pushedToPlaza = false,
  offlineBusy = false,
  error,
  onOffline,
  onBack,
}: {
  pageTitle: string;
  publication: MindSpacePublication;
  plazaPost?: PlazaPostBrief | null;
  pushedToPlaza?: boolean;
  offlineBusy?: boolean;
  error?: string | null;
  onOffline: () => void | Promise<void>;
  onBack: () => void;
}) {
  const publicPageUrl = resolvePublicPageUrl(publication.publicUrl);
  const plazaUrl = plazaPost ? resolvePlazaPostUrl(plazaPost.id) : resolvePlazaHomeUrl();

  return (
    <section className="mindspace-publish-success">
      <div className="mindspace-publish-success-card">
        <p className="mindspace-eyebrow">PUBLISHED</p>
        <h2>发布成功</h2>
        <p className="mindspace-publish-success-title">{pageTitle}</p>
        <a
          className="mindspace-publish-success-url"
          href={publicPageUrl}
          target="_blank"
          rel="noreferrer"
        >
          {publicPageUrl}
        </a>
        {pushedToPlaza ? (
          <p className="mindspace-publish-success-note">
            已推送到 Plaza。新发布内容会优先出现在广场首页前排。
          </p>
        ) : (
          <p className="mindspace-publish-success-note">
            尚未推送到 Plaza。公开页已生效，如需上广场请在页面详情中点击「发布到广场」。
          </p>
        )}
        {error ? <div className="mindspace-security-note">{error}</div> : null}
        <div className="mindspace-publish-success-actions">
          <a
            className="mindspace-primary"
            href={publicPageUrl}
            target="_blank"
            rel="noreferrer"
          >
            打开页面
          </a>
          <a href={plazaUrl} target="_blank" rel="noreferrer">
            {plazaPost ? '查看 Plaza 帖子' : '进 Plaza'}
          </a>
          <button type="button" onClick={() => void onOffline()} disabled={offlineBusy}>
            {offlineBusy ? '下线中…' : '立即下线'}
          </button>
        </div>
        <button type="button" className="mindspace-inline-back" onClick={onBack}>
          返回页面列表
        </button>
      </div>
    </section>
  );
}
