import type { GlobalAfterChangeHook } from 'payload'

import { revalidateTag } from 'next/cache'
import { auditLogger } from '@/utilities/auditLogger'

export const revalidateFooter: GlobalAfterChangeHook = async ({ doc, req }) => {
  const { payload, context } = req
  if (!context.disableRevalidate) {
    payload.logger.info(`Revalidating footer`)

    revalidateTag('global_footer')
  }

  // 記錄 Footer 變更
  if (req.user) {
    await auditLogger(req, {
      action: 'update',
      collection: 'global_footer',
      userId: req.user.id,
      userName: req.user.name as string,
      userEmail: req.user.email as string,
      userRole: req.user.role as string,
    })
  }

  return doc
}
