"use client";

import {
  getLessonPlansAction,
  getLessonPlanVersionsAction,
  revertLessonPlanVersionAction,
  duplicateLessonPlanAction,
  deleteLessonPlanAction,
} from "../actions";
import type { LessonPlanDataService } from "../providers/lesson-plan-provider";

/**
 * 默认数据服务实现：包装现有 Server Actions。
 * 通过 LessonPlanProvider 注入，组件不直接 import actions。
 * 测试时可替换为 mock 实现。
 */
export function createDefaultDataService(): LessonPlanDataService {
  return {
    async getLessonPlans(params) {
      const res = await getLessonPlansAction(params ?? {});
      if (res.success && res.data) {
        return { success: true, data: { items: res.data.items } };
      }
      return { success: false, message: res.message };
    },

    async getLessonPlanVersions(planId) {
      const res = await getLessonPlanVersionsAction(planId);
      if (res.success && res.data) {
        return { success: true, data: { versions: res.data.versions } };
      }
      return { success: false, message: res.message };
    },

    async revertLessonPlanVersion(params) {
      const res = await revertLessonPlanVersionAction(params);
      return { success: res.success, message: res.message };
    },

    async duplicateLessonPlan(planId) {
      const res = await duplicateLessonPlanAction(planId);
      return { success: res.success, message: res.message };
    },

    async deleteLessonPlan(planId) {
      const res = await deleteLessonPlanAction(planId);
      return { success: res.success, message: res.message };
    },
  };
}
