<script setup lang="ts">
import log from "loglevel";
import { COMPETENCE_NAVI_CERTIFICATE_QUERY } from "@/graphql/queries";
import { useQuery } from "@urql/vue";
import { computed, onMounted } from "vue";
import type { CompetenceCertificate } from "@/types";
import { useCurrentCourseSession } from "@/composables";
import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue";

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

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

const courseSession = useCurrentCourseSession();

const certificatesQuery = useQuery({
  query: COMPETENCE_NAVI_CERTIFICATE_QUERY,
  variables: {
    courseSlug: props.courseSlug,
    courseSessionId: courseSession.value.id,
  },
});

const certificate = computed(() => {
  return (
    (certificatesQuery.data.value?.competence_certificate_list
      ?.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
        class="btn-text inline-flex items-center pl-0"
        :to="`/course/${props.courseSlug}/competence/certificates`"
      >
        <it-icon-arrow-left />
        <span>{{ $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>
