import type { CourseSession } from "@/types";
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, vi } from "vitest";
import { useCourseSessionsStore } from "../courseSessions";
import { useUserStore } from "../user";

let user = {};
let courseSessions: CourseSession[] = [];

describe("CourseSession Store", () => {
  vi.mock("vue-router", () => ({
    useRoute: () => ({
      path: "/course/test-course/learn/",
    }),
  }));

  vi.mock("@/fetchHelpers", () => {
    const itGetCached = () => Promise.resolve([]);
    const itPost = () => Promise.resolve([]);

    return {
      itGetCached,
      itPost,
    };
  });

  beforeEach(() => {
    // creates a fresh pinia and make it active so it's automatically picked
    // up by any useStore() call without having to pass it to it:
    // `useStore(pinia)`
    setActivePinia(createPinia());
    user = {
      is_superuser: false,
      course_session_expert: [],
    };
    courseSessions = [
      {
        id: 1,
        created_at: "2021-05-11T10:00:00.000000Z",
        updated_at: "2023-05-11T10:00:00.000000Z",
        course: {
          id: 1,
          title: "Test Course",
          category_name: "Test Category",
          slug: "test-course",
        },
        title: "Test Course Session",
        start_date: "2022-05-11T10:00:00.000000Z",
        end_date: "2023-05-11T10:00:00.000000Z",
        learning_path_url: "/course/test-course/learn/",
        competence_url: "/course/test-course/competence/",
        course_url: "/course/test-course/",
        media_library_url: "/course/test-course/media/",
        attendance_courses: [],
        additional_json_data: {},
        documents: [],
      },
    ];
  });

  it("normal user has no cockpit", () => {
    const userStore = useUserStore();
    userStore.$patch(user);

    const courseSessionsStore = useCourseSessionsStore();
    courseSessionsStore._currentCourseSlug = "test-course";
    courseSessionsStore.allCourseSessions = courseSessions;

    expect(courseSessionsStore.currentCourseSessionHasCockpit).toBeFalsy();
  });

  it("superuser has cockpit", () => {
    const userStore = useUserStore();
    userStore.$patch(Object.assign(user, { is_superuser: true }));

    const courseSessionsStore = useCourseSessionsStore();
    courseSessionsStore._currentCourseSlug = "test-course";
    courseSessionsStore.allCourseSessions = courseSessions;

    expect(courseSessionsStore.currentCourseSessionHasCockpit).toBeTruthy();
  });

  it("expert has cockpit", () => {
    const userStore = useUserStore();
    userStore.$patch(
      Object.assign(user, { course_session_experts: [courseSessions[0].id] })
    );

    const courseSessionsStore = useCourseSessionsStore();
    courseSessionsStore._currentCourseSlug = "test-course";
    courseSessionsStore.allCourseSessions = courseSessions;

    expect(courseSessionsStore.currentCourseSessionHasCockpit).toBeTruthy();
  });
});
