<script setup lang="ts">
import AssignmentSubmissionProgress from "@/components/assignment/AssignmentSubmissionProgress.vue";
import FeedbackSubmissionProgress from "@/components/selfEvaluationFeedback/FeedbackSubmissionProgress.vue";
import {
  useCourseDataWithCompletion,
  useCourseSessionDetailQuery,
} from "@/composables";
import { circleFlatLearningContents } from "@/services/circle";
import type {
  CourseSession,
  LearningContent,
  LearningContentAssignment,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  LearningContentEdoniqTest,
} from "@/types";
import { learningContentTypeData } from "@/utils/typeMaps";
import { getCockpitUrl } from "@/utils/utils";
import { useTranslation } from "i18next-vue";
import log from "loglevel";
import { computed } from "vue";

interface Submittable {
  id: string;
  circleName: string;
  circleId: string;
  frontendUrl: string;
  title: string;
  showDetailsText: string;
  detailsLink: string;
  content: LearningContent;
}

const props = defineProps<{
  courseSession: CourseSession;
  selectedCircle: string;
}>();

log.debug("SubmissionsOverview created", props.courseSession.id);

const courseSessionDetailResult = useCourseSessionDetailQuery();

const { t } = useTranslation();

const lpQueryResult = useCourseDataWithCompletion();

const submittables = computed(() => {
  if (!lpQueryResult.circles.value?.length) {
    return [];
  }
  return lpQueryResult.circles.value
    .filter((circle) => props.selectedCircle == circle.id)
    .flatMap((circle) => {
      const learningContents = circleFlatLearningContents(circle).filter(
        (lc) =>
          lc.content_type === "learnpath.LearningContentAssignment" ||
          lc.content_type === "learnpath.LearningContentFeedbackUK" ||
          lc.content_type === "learnpath.LearningContentFeedbackVV" ||
          lc.content_type === "learnpath.LearningContentFeedbackAutomobilGewerbe" ||
          lc.content_type === "learnpath.LearningContentEdoniqTest"
      );

      return learningContents.map((lc) => {
        return {
          id: lc.id,
          circleName: circle.title,
          circleId: circle.id,
          frontendUrl: lc.frontend_url,
          title: getLearningContentType(lc),
          showDetailsText: getShowDetailsText(lc),
          detailsLink: getDetailsLink(lc, circle.id),
          content: lc,
        };
      });
    }) as Submittable[];
});

const isFeedback = (lc: LearningContent) => {
  return (
    lc.content_type === "learnpath.LearningContentFeedbackUK" ||
    lc.content_type === "learnpath.LearningContentFeedbackVV" ||
    lc.content_type === "learnpath.LearningContentFeedbackAutomobilGewerbe"
  );
};

const isAssignment = (lc: LearningContent) => {
  return lc.content_type === "learnpath.LearningContentAssignment";
};

const isEdoniqTest = (lc: LearningContent) => {
  return lc.content_type === "learnpath.LearningContentEdoniqTest";
};

const getLearningContentType = (lc: LearningContent) => {
  if (isAssignment(lc)) {
    const lcTypeData = learningContentTypeData(lc, t);
    if ((lc as LearningContentAssignment).assignment_type === "REFLECTION") {
      return lcTypeData.title;
    }
    return `${lcTypeData.title}: ${lc.title}`;
  } else if (isEdoniqTest(lc)) {
    return lc.title;
  } else if (isFeedback(lc)) {
    return t("Feedback: Feedback zum Lehrgang");
  }
  return "!!unknown!!";
};

const getShowDetailsText = (lc: LearningContent) => {
  if (isAssignment(lc)) {
    const assignmentType = (lc as LearningContentAssignment).assignment_type;
    if (
      assignmentType === "PRAXIS_ASSIGNMENT" ||
      assignmentType === "CASEWORK" ||
      assignmentType === "REFLECTION"
    ) {
      return t("a.Ergebnisse anschauen");
    } else if (
      assignmentType === "PREP_ASSIGNMENT" ||
      assignmentType === "CONDITION_ACCEPTANCE"
    ) {
      return t("Status anschauen");
    }
  } else if (isEdoniqTest(lc)) {
    return t("a.Ergebnisse anschauen");
  } else if (isFeedback(lc)) {
    return t("Feedback anschauen");
  }
  return "!!unknown!!";
};

const getDetailsLink = (lc: LearningContent, circleId: string) => {
  const base = getCockpitUrl(props.courseSession.course.slug);

  if (isFeedback(lc)) {
    return `${base}/feedback/${circleId}`;
  } else if (isAssignment(lc) || isEdoniqTest(lc)) {
    return `${base}/assignment/${lc.id}`;
  }
  return "";
};

const getIconName = (lc: LearningContent) => {
  if (isAssignment(lc)) {
    const assignmentType = (lc as LearningContentAssignment).assignment_type;
    if (
      assignmentType === "PREP_ASSIGNMENT" ||
      assignmentType === "CASEWORK" ||
      assignmentType === "PRAXIS_ASSIGNMENT" ||
      assignmentType === "CONDITION_ACCEPTANCE"
    ) {
      return "it-icon-assignment-large";
    } else if (assignmentType === "REFLECTION") {
      return "it-icon-test-large";
    }
  }
  return "it-icon-feedback-large";
};
</script>

<template>
  <div class="bg-white px-6 py-2">
    <div
      v-if="courseSessionDetailResult.filterMembers().length"
      class="divide-y divide-gray-500"
    >
      <div
        v-for="submittable in submittables"
        :key="submittable.id"
        class="flex flex-col justify-between gap-2 py-4 lg:flex-row lg:gap-4"
        :data-cy="`submittable-${submittable.content.slug}`"
      >
        <div class="flex flex-row items-center gap-2 lg:w-1/3">
          <div class="min-h-9 min-w-9">
            <component
              :is="getIconName(submittable.content)"
              class="min-h-9 min-w-9"
            ></component>
          </div>
          <div class="flex flex-col">
            <h3 class="text-bold flex items-center gap-2">{{ submittable.title }}</h3>
            <p class="text-gray-800">
              {{ $t("a.Circle") }} «{{ submittable.circleName }}»
            </p>
          </div>
        </div>
        <AssignmentSubmissionProgress
          v-if="isAssignment(submittable.content) || isEdoniqTest(submittable.content)"
          :course-session="props.courseSession"
          :learning-content="
            submittable.content as LearningContentAssignment | LearningContentEdoniqTest
          "
          :show-title="false"
          class="grow pr-8"
        />
        <FeedbackSubmissionProgress
          v-if="isFeedback(submittable.content)"
          :course-session="props.courseSession"
          :circle-id="submittable.circleId"
          class="grow pr-8"
        ></FeedbackSubmissionProgress>
        <div class="flex items-center lg:w-1/4 lg:justify-end">
          <router-link
            v-if="submittable.detailsLink"
            class="btn-primary"
            :to="submittable.detailsLink"
            :data-cy="
              isFeedback(submittable.content)
                ? `show-feedback-btn-${submittable.content.slug}`
                : `show-details-btn-${submittable.content.slug}`
            "
          >
            {{ submittable.showDetailsText }}
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>
