"use client"

/**
 * 教材模块埋点接口（预留）。
 *
 * 当前为 no-op 实现，后续接入真实监控 SDK 时只需替换 Provider。
 * 通过 React Context 注入，组件内调用 `useTextbookAnalytics()` 获取。
 */

import { createContext, useContext, type ReactNode } from "react"

export interface TextbookAnalytics {
  /** 教材被打开时触发 */
  onTextbookOpen?(textbookId: string): void
  /** 章节被阅读时触发（含停留时长） */
  onChapterRead?(textbookId: string, chapterId: string, durationMs: number): void
  /** 知识点被点击时触发 */
  onKnowledgePointClick?(kpId: string): void
  /** 知识点被创建时触发 */
  onKnowledgePointCreate?(chapterId: string, kpId: string): void
  /** 章节内容被编辑保存时触发 */
  onChapterContentUpdate?(chapterId: string): void
}

const TextbookAnalyticsContext = createContext<TextbookAnalytics>({})

export function TextbookAnalyticsProvider({
  analytics,
  children,
}: {
  analytics?: TextbookAnalytics
  children: ReactNode
}) {
  return (
    <TextbookAnalyticsContext.Provider value={analytics ?? {}}>
      {children}
    </TextbookAnalyticsContext.Provider>
  )
}

export function useTextbookAnalytics(): TextbookAnalytics {
  return useContext(TextbookAnalyticsContext)
}
