import type { ReactNode } from "react";
import { Button, DismissibleAlert, LoadingFrame, PageTitle } from "@govoplan/core-webui";
import type { ApiSettings } from "../../../types";
import type { CampaignVersionDetail, CampaignVersionListItem } from "../../../api/campaigns";
import LockedVersionNotice from "./LockedVersionNotice";
import VersionLine from "./VersionLine";

type CampaignDraftPageScaffoldProps = {
  settings: ApiSettings;
  campaignId: string;
  title: string;
  loading: boolean;
  version: CampaignVersionDetail | null;
  versions: CampaignVersionListItem[];
  saveState: string;
  onReload: () => Promise<void>;
  onSave: () => void;
  dirty: boolean;
  locked: boolean;
  draft: Record<string, unknown> | null;
  error?: string | null;
  localError?: string | null;
  currentVersionId?: string | null;
  children: ReactNode;
};

export default function CampaignDraftPageScaffold({
  settings,
  campaignId,
  title,
  loading,
  version,
  versions,
  saveState,
  onReload,
  onSave,
  dirty,
  locked,
  draft,
  error,
  localError,
  currentVersionId,
  children
}: CampaignDraftPageScaffoldProps) {
  return (
    <div className="content-pad workspace-data-page">
      <div className="page-heading split workspace-heading">
        <div>
          <PageTitle loading={loading}>{title}</PageTitle>
          <VersionLine version={version} versions={versions} status={saveState} />
        </div>
        <div className="button-row compact-actions">
          <Button onClick={onReload} disabled={loading}>Discard</Button>
          <Button variant="primary" onClick={onSave} disabled={!dirty || locked || !draft}>i18n:govoplan-campaign.save.efc007a3</Button>
        </div>
      </div>

      {error && <DismissibleAlert tone="danger" resetKey={error} floating>{error}</DismissibleAlert>}
      {localError && <DismissibleAlert tone="danger" resetKey={localError} floating>{localError}</DismissibleAlert>}
      {locked && <LockedVersionNotice settings={settings} campaignId={campaignId} version={version} currentVersionId={currentVersionId} reload={onReload} message="i18n:govoplan-campaign.this_page_is_read_only_for_the_selected_version.dacf5743" />}

      <LoadingFrame loading={loading || !draft} label="i18n:govoplan-campaign.loading_campaign_draft.1cf47e50">
        <>{children}</>
      </LoadingFrame>
    </div>);

}
