<template>
  <main class="h-screen-main relative">
    <div class="h-full relative bg-neutral-50">
      <ConformanceSidebar></ConformanceSidebar>
      <ConformanceResults></ConformanceResults>
    </div>
    <StatusBar></StatusBar>
  </main>
</template>
<script setup>
// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
//   imacat.yang@dsp.im (imacat), 2023/9/23
/**
 * @module views/Discover/Conformance Conformance checking page
 *   with sidebar rule configuration and results display.
 */

import { onMounted, onBeforeUnmount } from "vue";
import { useRoute } from "vue-router";
import { storeToRefs } from "pinia";
import { useLoadingStore } from "@/stores/loading";
import { useConformanceStore } from "@/stores/conformance";
import StatusBar from "@/components/Discover/StatusBar.vue";
import ConformanceResults from "@/components/Discover/Conformance/ConformanceResults.vue";
import ConformanceSidebar from "@/components/Discover/Conformance/ConformanceSidebar.vue";

defineOptions({
  async beforeRouteEnter(to, from, next) {
    const isCheckPage = to.name.includes("Check");
    if (isCheckPage) {
      const conformanceStore = useConformanceStore();
      switch (to.params.type) {
        case "log":
          conformanceStore.setConformanceLogCreateCheckId(to.params.fileId);
          break;
        case "filter":
          conformanceStore.conformanceFilterCreateCheckId = to.params.fileId;
          break;
      }
      await conformanceStore.getConformanceReport();
      to.meta.file = await conformanceStore.conformanceTempReportData?.file;
    }
    next();
  },
});

const route = useRoute();

// Stores
const loadingStore = useLoadingStore();
const conformanceStore = useConformanceStore();
const { isLoading } = storeToRefs(loadingStore);
const {
  conformanceLogId,
  conformanceFilterId,
  conformanceLogCreateCheckId,
  conformanceFilterCreateCheckId,
  conformanceLogTempCheckId,
  conformanceFilterTempCheckId,
  selectedRuleType,
  selectedActivitySequence,
  selectedMode,
  selectedProcessScope,
  selectedActSeqMore,
  selectedActSeqFromTo,
  conformanceRuleData,
  conformanceTempReportData,
  conformanceFileName,
} = storeToRefs(conformanceStore);

let loadingTimerId = null;

onMounted(async () => {
  isLoading.value = true;
  try {
    const params = route.params;
    const file = route.meta.file;
    const isCheckPage = route.name.includes("Check");

    if (isCheckPage) {
      switch (params.type) {
        case "log":
          conformanceLogId.value = file.parent?.id;
          conformanceFileName.value = file.name;
          break;
        case "filter":
          conformanceFilterId.value = file.parent?.id;
          conformanceFileName.value = file.name;
          break;
      }
      await conformanceStore.getConformanceReport();
    } else {
      switch (params.type) {
        case "log":
          conformanceLogId.value = params.fileId;
          break;
        case "filter":
          conformanceFilterId.value = params.fileId;
          break;
      }
    }
    await conformanceStore.getConformanceParams();
  } catch (error) {
    console.error("Failed to initialize conformance:", error);
  } finally {
    loadingTimerId = setTimeout(() => (isLoading.value = false), 500);
  }
  selectedRuleType.value = "Have activity";
  selectedActivitySequence.value = "Start & End";
  selectedMode.value = "Directly follows";
  selectedProcessScope.value = "End to end";
  selectedActSeqMore.value = "All";
  selectedActSeqFromTo.value = "From";
});

onBeforeUnmount(() => {
  clearTimeout(loadingTimerId);
  conformanceLogId.value = null;
  conformanceFilterId.value = null;
  conformanceLogCreateCheckId.value = null;
  conformanceFilterCreateCheckId.value = null;
  conformanceRuleData.value = null;
  conformanceFileName.value = null;
});
</script>
