import { Language, getStoredLanguage } from "@/lib/language";

export const COURSE_ID_STORAGE_KEY = "course_id";
export const COURSE_MAP_STORAGE_KEY = "course_id_by_language";
export const ACTIVE_COURSE_LANGUAGE_STORAGE_KEY = "active_course_language";

type CourseLike = {
  id?: string;
  language?: string | number;
};

const getCourseLanguage = (courseLanguage?: string | number): Language | null => {
  const value = String(courseLanguage ?? "");

  // Backend course language ids are reversed from quiz/test lang ids.
  if (value === "1") return "hindi";
  if (value === "2") return "english";

  return null;
};

export const buildCourseMap = (courses: CourseLike[] = []) => {
  return courses.reduce(
    (acc, course) => {
      const language = getCourseLanguage(course.language);

      if (language && course.id) {
        acc[language] = String(course.id);
      }

      return acc;
    },
    {} as Partial<Record<Language, string>>,
  );
};

export const persistCourseMap = (courses: CourseLike[] = []) => {
  if (typeof window === "undefined") return {};

  const courseMap = buildCourseMap(courses);
  localStorage.setItem(COURSE_MAP_STORAGE_KEY, JSON.stringify(courseMap));

  const activeLanguage =
    getActiveCourseLanguage() ?? getStoredLanguage() ?? "english";
  const activeCourseId =
    courseMap[activeLanguage] ?? courseMap.english ?? courseMap.hindi;

  if (!localStorage.getItem(ACTIVE_COURSE_LANGUAGE_STORAGE_KEY)) {
    const fallbackLanguage = courseMap.english ? "english" : "hindi";
    localStorage.setItem(ACTIVE_COURSE_LANGUAGE_STORAGE_KEY, fallbackLanguage);
  }

  if (activeCourseId) {
    localStorage.setItem(COURSE_ID_STORAGE_KEY, activeCourseId);
  }

  return courseMap;
};

export const getStoredCourseMap = (): Partial<Record<Language, string>> => {
  if (typeof window === "undefined") return {};

  try {
    const raw = localStorage.getItem(COURSE_MAP_STORAGE_KEY);
    return raw ? JSON.parse(raw) : {};
  } catch {
    return {};
  }
};

export const getCourseIdForLanguage = (language: Language) => {
  const courseMap = getStoredCourseMap();
  return courseMap[language] ?? null;
};

export const getActiveCourseLanguage = (): Language | null => {
  if (typeof window === "undefined") return null;

  const value = localStorage.getItem(ACTIVE_COURSE_LANGUAGE_STORAGE_KEY);
  return value === "hindi" || value === "english" ? value : null;
};

export const setActiveCourseLanguage = (language: Language) => {
  if (typeof window === "undefined") return null;

  localStorage.setItem(ACTIVE_COURSE_LANGUAGE_STORAGE_KEY, language);
  return setActiveCourseId(language);
};

export const setActiveCourseId = (language: Language) => {
  if (typeof window === "undefined") return null;

  const courseId = getCourseIdForLanguage(language);

  if (courseId) {
    localStorage.setItem(COURSE_ID_STORAGE_KEY, courseId);
  }

  return courseId;
};

export const getActiveCourseId = (language?: Language) => {
  if (typeof window === "undefined") return null;

  const preferredLanguage = language ?? getActiveCourseLanguage() ?? "english";
  const mappedCourseId = getCourseIdForLanguage(preferredLanguage);

  if (mappedCourseId) {
    localStorage.setItem(COURSE_ID_STORAGE_KEY, mappedCourseId);
    return mappedCourseId;
  }

  return localStorage.getItem(COURSE_ID_STORAGE_KEY);
};

export const getCourseForLanguage = <T extends CourseLike>(
  courses: T[],
  language: Language,
): T | null => {
  return (
    courses.find((course) => getCourseLanguage(course.language) === language) ??
    courses[0] ??
    null
  );
};
