<script setup lang="ts">
import { relativeTimeTo } from "@/utils/date";
import type { ListedSnapshotDto, SinglePage } from "@halo-dev/api-client";
import { consoleApiClient } from "@halo-dev/api-client";
import { Dialog, Toast, VButton, VStatusDot, VTag } from "@halo-dev/components";
import { useQueryClient } from "@tanstack/vue-query";
import { computed } from "vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const queryClient = useQueryClient();

const props = withDefaults(
  defineProps<{
    singlePage?: SinglePage;
    snapshot: ListedSnapshotDto;
    selectedSnapshotName?: string;
  }>(),
  {
    singlePage: undefined,
    selectedSnapshotName: undefined,
  }
);

async function handleRestore() {
  Dialog.warning({
    title: t("core.page_snapshots.operations.revert.title"),
    description: t("core.page_snapshots.operations.revert.description"),
    confirmText: t("core.common.buttons.confirm"),
    cancelText: t("core.common.buttons.cancel"),
    async onConfirm() {
      await consoleApiClient.content.singlePage.revertToSpecifiedSnapshotForSinglePage(
        {
          name: props.singlePage?.metadata.name as string,
          revertSnapshotForSingleParam: {
            snapshotName: props.snapshot.metadata.name,
          },
        }
      );
      await queryClient.invalidateQueries({
        queryKey: ["singlePage-snapshots-by-singlePage-name"],
      });
      Toast.success(t("core.page_snapshots.operations.revert.toast_success"));
    },
  });
}

function handleDelete() {
  Dialog.warning({
    title: t("core.page_snapshots.operations.delete.title"),
    description: t("core.page_snapshots.operations.delete.description"),
    confirmText: t("core.common.buttons.confirm"),
    cancelText: t("core.common.buttons.cancel"),
    async onConfirm() {
      await consoleApiClient.content.singlePage.deleteSinglePageContent({
        name: props.singlePage?.metadata.name as string,
        snapshotName: props.snapshot.metadata.name,
      });
      await queryClient.invalidateQueries({
        queryKey: ["singlePage-snapshots-by-singlePage-name"],
      });
      Toast.success(t("core.common.toast.delete_success"));
    },
  });
}

const isSelected = computed(() => {
  return props.selectedSnapshotName === props.snapshot.metadata.name;
});

const isReleased = computed(() => {
  return (
    props.singlePage?.spec.releaseSnapshot === props.snapshot.metadata.name
  );
});

const isHead = computed(() => {
  const { headSnapshot, releaseSnapshot } = props.singlePage?.spec || {};
  return (
    headSnapshot !== releaseSnapshot &&
    headSnapshot === props.snapshot.metadata.name
  );
});

const isBase = computed(() => {
  return props.singlePage?.spec.baseSnapshot === props.snapshot.metadata.name;
});
</script>
<template>
  <div
    class="group relative flex cursor-pointer flex-col gap-5 p-4"
    :class="{ 'bg-gray-50': isSelected }"
  >
    <div
      v-if="isSelected"
      class="absolute inset-y-0 left-0 w-0.5 bg-primary"
    ></div>
    <div class="flex items-center justify-between">
      <div
        class="truncate text-sm"
        :class="{
          'font-semibold': isSelected,
        }"
      >
        {{ relativeTimeTo(snapshot.metadata.creationTimestamp) }}
      </div>
      <div class="inline-flex flex-none items-center space-x-3">
        <VTag v-if="isReleased" theme="primary">
          {{ $t("core.page_snapshots.status.released") }}
        </VTag>
        <VTag v-if="isHead">
          {{ $t("core.page_snapshots.status.draft") }}
        </VTag>
        <VTag v-if="isBase">
          {{ $t("core.page_snapshots.status.base") }}
        </VTag>
        <VStatusDot
          v-if="snapshot.metadata.deletionTimestamp"
          v-tooltip="$t('core.common.status.deleting')"
          state="warning"
          animate
        />
      </div>
    </div>
    <div class="flex h-6 items-end justify-between gap-2">
      <div class="flex-1 truncate text-xs text-gray-600">
        {{ snapshot.spec.owner }}
      </div>
      <div
        v-if="!isReleased"
        class="hidden flex-none space-x-2 group-hover:block"
      >
        <VButton v-if="!isHead" size="xs" @click="handleRestore()">
          {{ $t("core.page_snapshots.operations.revert.button") }}
        </VButton>
        <VButton v-if="!isBase" size="xs" type="danger" @click="handleDelete">
          {{ $t("core.common.buttons.delete") }}
        </VButton>
      </div>
    </div>
  </div>
</template>
