import { formatDateTime, statusTypeMap } from "@/module/experiment/model/experiment-view-model";

export const mapReleaseDetail = (item = {}) => ({
  id: item.id || "",
  experimentId: item.experiment_id || "",
  versionNo: item.version_no ?? 0,
  status: item.status || "draft",
  runtimePayload: item.runtime_payload || {},
});

export const buildReleaseStatusTag = (status) => statusTypeMap[status] || "info";

export const buildReleaseTitle = (release) =>
  release?.versionNo ? `版本 v${release.versionNo}` : "未命名版本";

export const buildRuntimePayloadSummary = (payload = {}) => {
  const topLevelKeys = Object.keys(payload || {});
  const variants = Array.isArray(payload?.variants) ? payload.variants.length : 0;

  return {
    keyCount: topLevelKeys.length,
    variants,
    keys: topLevelKeys,
  };
};

export const buildReleaseReadiness = ({ release, payloadSummary }) => {
  if (!release?.id) {
    return {
      level: "warning",
      title: "版本尚未就緒",
      description: "先確認版本已建立，再進行執行內容與發佈檢查。",
    };
  }

  if (payloadSummary.keyCount === 0) {
    return {
      level: "warning",
      title: "這個版本尚未包含執行內容",
      description: "先確認版本是否已正確建立執行內容，之後才能往發佈與套用驗證走。",
    };
  }

  return {
    level: "ready",
    title: "這個版本已具備執行內容",
    description: "可以發佈此版本，讓頁面變更上線並開始追蹤成效。",
  };
};

export const buildReleaseUpdatedHint = (value) =>
  value ? formatDateTime(value) : "目前尚未記錄更新時間";
