import type { AuditService } from "./audit-service"
import type {
  AuditLog,
  AuditOverviewStats,
  AuditRetentionConfig,
  AuditTrendPoint,
  DataChangeActionStat,
  DataChangeLog,
  DataChangeStat,
  LoginLog,
  PaginatedResult,
  PurgeResult,
} from "@/modules/audit/types"
import {
  getAuditRetentionConfigAction,
  saveAuditRetentionConfigAction,
  purgeAuditLogsAction,
} from "../actions"

/**
 * 管理员审计服务实现（Client-safe）。
 *
 * 不 import server-only 的 data-access，而是委托 Server Actions 执行写操作。
 * 适用于通过 `AuditServiceProvider` 注入到 Client Component 中。
 *
 * 查询方法抛出错误，因为客户端组件应通过 RSC props 接收查询数据，
 * 而非直接调用查询接口。这强制执行正确的数据流：
 * - 读：RSC 页面调 data-access → props 传入 Client Component
 * - 写：Client Component → useAuditService() → Server Action
 *
 * 未来可新增 ComplianceAuditService（只读子集）等角色实现。
 */
export const adminAuditService: AuditService = {
  // ── 查询方法：客户端不可用，应通过 RSC props 传入 ──
  getAuditLogs: async (): Promise<PaginatedResult<AuditLog>> => {
    throw new Error("getAuditLogs is not available on client. Use RSC props.")
  },
  getLoginLogs: async (): Promise<PaginatedResult<LoginLog>> => {
    throw new Error("getLoginLogs is not available on client. Use RSC props.")
  },
  getDataChangeLogs: async (): Promise<PaginatedResult<DataChangeLog>> => {
    throw new Error("getDataChangeLogs is not available on client. Use RSC props.")
  },
  getAuditModuleOptions: async (): Promise<string[]> => {
    throw new Error("getAuditModuleOptions is not available on client. Use RSC props.")
  },
  getDataChangeStats: async (): Promise<DataChangeStat[]> => {
    throw new Error("getDataChangeStats is not available on client. Use RSC props.")
  },
  getDataChangeTableOptions: async (): Promise<string[]> => {
    throw new Error("getDataChangeTableOptions is not available on client. Use RSC props.")
  },
  getAuditOverviewStats: async (): Promise<AuditOverviewStats> => {
    throw new Error("getAuditOverviewStats is not available on client. Use RSC props.")
  },
  getAuditTrend: async (): Promise<AuditTrendPoint[]> => {
    throw new Error("getAuditTrend is not available on client. Use RSC props.")
  },
  getDataChangeActionStats: async (): Promise<DataChangeActionStat[]> => {
    throw new Error("getDataChangeActionStats is not available on client. Use RSC props.")
  },

  // ── 保留策略：通过 Server Action 委托（Client-safe） ──
  getAuditRetentionConfig: async (): Promise<AuditRetentionConfig> => {
    const res = await getAuditRetentionConfigAction()
    if (!res.success || !res.data) {
      throw new Error(res.message ?? "Failed to get audit retention config")
    }
    return res.data
  },
  saveAuditRetentionConfig: async (config: AuditRetentionConfig): Promise<void> => {
    const res = await saveAuditRetentionConfigAction(config)
    if (!res.success || !res.data) {
      throw new Error(res.message ?? "Failed to save audit retention config")
    }
  },
  purgeExpiredAuditLogs: async (
    retentionDays: number,
    loginLogRetentionDays?: number,
  ): Promise<PurgeResult> => {
    const res = await purgeAuditLogsAction(retentionDays, loginLogRetentionDays)
    if (!res.success || !res.data) {
      throw new Error(res.message ?? "Failed to purge audit logs")
    }
    return res.data
  },
}
