<script setup lang="ts">
import log from "loglevel";
import { computed, onMounted } from "vue";
import type { CompetenceCertificate } from "@/types";
import { useCertificateQuery } from "@/composables";
import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue";
import { getCertificates } from "@/services/competence";
import { getPreviousRoute } from "@/router/history";

const props = defineProps<{
  courseSlug: string;
  certificateSlug: string;
  userId?: string;
}>();

log.debug("CompetenceCertificateDetailPage setup", props);

const certificatesQuery = useCertificateQuery(
  props.userId,
  props.courseSlug
).certificatesQuery;

const certificate = computed(() => {
  const certificates = getCertificates(
    certificatesQuery.data.value,
    props.userId ?? null
  );

  if (!certificates) {
    return null;
  }

  return (
    (certificates.competence_certificates as unknown as CompetenceCertificate[]) ?? []
  ).find((cc) => cc.slug.endsWith(props.certificateSlug));
});

onMounted(async () => {
  // log.debug("AssignmentView mounted", props.assignmentId, props.userId);
});
</script>

<template>
  <div class="container-large">
    <nav class="py-4">
      <router-link
        :to="
          getPreviousRoute() || `/course/${props.courseSlug}/competence/certificates`
        "
        class="btn-text inline-flex items-center p-0"
        data-cy="back-button"
      >
        <it-icon-arrow-left class="-ml-1 mr-1 h-5 w-5"></it-icon-arrow-left>
        <span class="inline">{{ $t("general.back") }}</span>
      </router-link>
    </nav>
    <div v-if="certificate">
      <CompetenceCertificateComponent
        :competence-certificate="certificate"
        :detail-view="true"
      ></CompetenceCertificateComponent>
    </div>
  </div>
</template>

<style scoped></style>
