export const lessonContentTypes = ["VIDEO", "LECTURE", "ACTIVITY", "QUIZ", "FINAL_EXAM"] as const;

export type LessonContentType = (typeof lessonContentTypes)[number];

type LessonDescriptionMeta = {
  text: string;
  contentType: LessonContentType;
  materialUrl: string | null;
};

const lessonTypeAliases: Record<string, LessonContentType> = {
  VIDEO: "VIDEO",
  LECTURE: "LECTURE",
  LECTURA: "LECTURE",
  READING: "LECTURE",
  ACTIVITY: "ACTIVITY",
  ACTIVIDAD: "ACTIVITY",
  QUIZ: "QUIZ",
  EVALUACION: "QUIZ",
  EVALUACIÓN: "QUIZ",
  FINAL_EXAM: "FINAL_EXAM",
  EXAMEN_FINAL: "FINAL_EXAM",
  EXAMENFINAL: "FINAL_EXAM",
  EVALUACION_FINAL: "FINAL_EXAM",
  EVALUACIÓN_FINAL: "FINAL_EXAM",
};

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function asString(value: unknown): string {
  return typeof value === "string" ? value.trim() : "";
}

function normalizeType(value: string): LessonContentType {
  const normalized = value.trim().toUpperCase();
  return lessonTypeAliases[normalized] ?? "VIDEO";
}

function getDescriptionText(input: unknown): string {
  if (typeof input === "string") return input.trim();
  if (isRecord(input)) {
    const direct = asString(input.text);
    if (direct) return direct;
    const es = asString(input.es);
    if (es) return es;
    const en = asString(input.en);
    if (en) return en;
    const summary = asString(input.summary);
    if (summary) return summary;
  }
  return "";
}

export function parseLessonDescriptionMeta(description: unknown): LessonDescriptionMeta {
  if (!isRecord(description)) {
    return {
      text: getDescriptionText(description),
      contentType: "VIDEO",
      materialUrl: null,
    };
  }

  const contentTypeRaw = asString(description.contentType) || asString(description.kind) || asString(description.type);
  const materialUrl =
    asString(description.materialUrl) ||
    asString(description.resourceUrl) ||
    asString(description.pdfUrl) ||
    asString(description.attachmentUrl) ||
    "";

  return {
    text: getDescriptionText(description),
    contentType: normalizeType(contentTypeRaw || "VIDEO"),
    materialUrl: materialUrl || null,
  };
}

export function buildLessonDescriptionMeta(input: {
  text: string;
  contentType: LessonContentType;
  materialUrl?: string | null;
}): Record<string, string> {
  const payload: Record<string, string> = {
    contentType: input.contentType,
  };

  const text = input.text.trim();
  if (text) payload.es = text;

  const materialUrl = (input.materialUrl ?? "").trim();
  if (materialUrl) payload.materialUrl = materialUrl;

  return payload;
}

export function getLessonContentTypeLabel(contentType: LessonContentType): string {
  if (contentType === "LECTURE") return "Lectura";
  if (contentType === "ACTIVITY") return "Actividad";
  if (contentType === "QUIZ") return "Quiz";
  if (contentType === "FINAL_EXAM") return "Evaluación final";
  return "Video";
}

export function isFinalExam(contentType: LessonContentType): boolean {
  return contentType === "FINAL_EXAM";
}
