// src/hooks/useRecommendedLessons.ts
import { useState, useCallback } from "react";
import { z } from "zod";
import dayjs from "dayjs";
var recommendedClassSubjectSchema = z.object({
  id: z.string().uuid(),
  name: z.string()
}).nullable();
var recommendedClassCreatorSchema = z.object({
  id: z.string().uuid(),
  name: z.string()
}).nullable();
var recommendedClassStatsSchema = z.object({
  totalStudents: z.number(),
  completedCount: z.number(),
  completionPercentage: z.number()
});
var recommendedClassBreakdownSchema = z.object({
  classId: z.string().uuid(),
  className: z.string(),
  schoolId: z.string(),
  schoolName: z.string(),
  studentCount: z.number(),
  completedCount: z.number()
});
var recommendedClassDataSchema = z.object({
  id: z.string().uuid(),
  title: z.string(),
  startDate: z.string().nullable(),
  finalDate: z.string().nullable(),
  createdAt: z.string(),
  progress: z.number(),
  totalLessons: z.number()
});
var recommendedClassHistoryItemSchema = z.object({
  recommendedClass: recommendedClassDataSchema,
  subject: recommendedClassSubjectSchema,
  creator: recommendedClassCreatorSchema,
  stats: recommendedClassStatsSchema,
  breakdown: z.array(recommendedClassBreakdownSchema)
});
var recommendedClassHistoryApiResponseSchema = z.object({
  message: z.string(),
  data: z.object({
    recommendedClass: z.array(recommendedClassHistoryItemSchema),
    total: z.number()
  })
});
var determineRecommendedClassStatus = (finalDate, completionPercentage) => {
  if (completionPercentage === 100) {
    return "CONCLU\xCDDA" /* CONCLUIDA */;
  }
  if (finalDate) {
    const now = dayjs();
    const deadline = dayjs(finalDate);
    if (deadline.isBefore(now)) {
      return "VENCIDA" /* VENCIDA */;
    }
  }
  return "ATIVA" /* ATIVA */;
};
var transformRecommendedClassToTableItem = (item) => {
  const firstBreakdown = item.breakdown[0];
  const schoolName = firstBreakdown?.schoolName || "-";
  const className = firstBreakdown?.className || "-";
  const classDisplay = item.breakdown.length > 1 ? `${item.breakdown.length} turmas` : className;
  return {
    id: item.recommendedClass.id,
    startDate: item.recommendedClass.startDate ? dayjs(item.recommendedClass.startDate).format("DD/MM") : "-",
    deadline: item.recommendedClass.finalDate ? dayjs(item.recommendedClass.finalDate).format("DD/MM") : "-",
    title: item.recommendedClass.title,
    school: schoolName,
    year: "-",
    // API doesn't provide year directly
    creator: item.creator?.name || "-",
    subject: item.subject?.name || "-",
    class: classDisplay,
    status: determineRecommendedClassStatus(
      item.recommendedClass.finalDate,
      item.stats.completionPercentage
    ),
    completionPercentage: item.stats.completionPercentage
  };
};
var handleRecommendedClassFetchError = (error) => {
  if (error instanceof z.ZodError) {
    console.error("Erro ao validar dados de hist\xF3rico de aulas:", error);
    return "Erro ao validar dados de hist\xF3rico de aulas";
  }
  console.error("Erro ao carregar hist\xF3rico de aulas:", error);
  return "Erro ao carregar hist\xF3rico de aulas";
};
var createUseRecommendedLessonsHistory = (fetchRecommendedClassHistory) => {
  return () => {
    const [state, setState] = useState({
      recommendedClass: [],
      loading: false,
      error: null,
      pagination: {
        total: 0,
        page: 1,
        limit: 10,
        totalPages: 0
      }
    });
    const fetchRecommendedClass = useCallback(
      async (filters) => {
        setState((prev) => ({ ...prev, loading: true, error: null }));
        try {
          const responseData = await fetchRecommendedClassHistory(filters);
          const validatedData = recommendedClassHistoryApiResponseSchema.parse(responseData);
          const tableItems = validatedData.data.recommendedClass.map(
            transformRecommendedClassToTableItem
          );
          const page = filters?.page || 1;
          const limit = filters?.limit || 10;
          const total = validatedData.data.total;
          const totalPages = Math.ceil(total / limit);
          setState({
            recommendedClass: tableItems,
            loading: false,
            error: null,
            pagination: {
              total,
              page,
              limit,
              totalPages
            }
          });
        } catch (error) {
          const errorMessage = handleRecommendedClassFetchError(error);
          setState((prev) => ({
            ...prev,
            loading: false,
            error: errorMessage
          }));
        }
      },
      [fetchRecommendedClassHistory]
    );
    return {
      ...state,
      fetchRecommendedClass
    };
  };
};
var createRecommendedLessonsHistoryHook = createUseRecommendedLessonsHistory;
export {
  createRecommendedLessonsHistoryHook,
  createUseRecommendedLessonsHistory,
  determineRecommendedClassStatus,
  handleRecommendedClassFetchError,
  recommendedClassHistoryApiResponseSchema,
  transformRecommendedClassToTableItem
};
//# sourceMappingURL=index.mjs.map