// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { cloneDeep } from 'lodash-es'

import { useOnlineNotificationDeleteMutation } from '#shared/entities/online-notification/graphql/mutations/delete.api.ts'
import { useOnlineNotificationMarkAllAsSeenMutation } from '#shared/entities/online-notification/graphql/mutations/markAllAsSeen.api.ts'
import { useOnlineNotificationSeenMutation } from '#shared/entities/online-notification/graphql/mutations/seen.api.ts'
import { OnlineNotificationsDocument } from '#shared/entities/online-notification/graphql/queries/onlineNotifications.api.ts'
import type {
  OnlineNotification,
  OnlineNotificationsQuery,
  Scalars,
} from '#shared/graphql/types.ts'
import { getApolloClient } from '#shared/server/apollo/client.ts'
import { MutationHandler } from '#shared/server/apollo/handler/index.ts'

import { useOnlineNotificationDeleteAllMutation } from '../graphql/mutations/deleteAll.api.ts'

export const useOnlineNotificationActions = () => {
  const { cache } = getApolloClient()

  const getCacheData = () => {
    const queryOptions = {
      query: OnlineNotificationsDocument,
    }
    const existingQueryCache = cache.readQuery<OnlineNotificationsQuery>(queryOptions)

    if (!existingQueryCache?.onlineNotifications) return null

    const oldQueryCache = cloneDeep(existingQueryCache)

    return { queryOptions, oldQueryCache, existingQueryCache }
  }

  const removeNotificationCacheUpdate = (id: Scalars['ID']['output']) => {
    const data = getCacheData()

    if (!data) return

    const { queryOptions, oldQueryCache, existingQueryCache } = data

    cache.writeQuery({
      ...queryOptions,
      data: {
        onlineNotifications: {
          edges: existingQueryCache.onlineNotifications.edges.filter((edge) => edge.node.id !== id),
          pageInfo: existingQueryCache.onlineNotifications.pageInfo,
        },
      },
    })

    return () => {
      cache.writeQuery({
        ...queryOptions,
        data: oldQueryCache,
      })
    }
  }

  const updateAllSeenNotificationCache = () => {
    const data = getCacheData()

    if (!data) return

    const { queryOptions, oldQueryCache, existingQueryCache } = data

    const clonedQueryCache = cloneDeep(existingQueryCache)

    clonedQueryCache.onlineNotifications.edges.forEach(({ node }) => {
      node.seen = true
    })

    cache.writeQuery({
      ...queryOptions,
      data: {
        onlineNotifications: {
          ...clonedQueryCache.onlineNotifications,
        },
      },
    })

    return () => {
      cache.writeQuery({
        ...queryOptions,
        data: oldQueryCache,
      })
    }
  }

  const updateSeenNotificationCache = (id: Scalars['ID']['output']) => {
    const data = getCacheData()

    if (!data) return

    const { queryOptions, oldQueryCache, existingQueryCache } = data

    const clonedQueryCache = cloneDeep(existingQueryCache)

    clonedQueryCache.onlineNotifications.edges.forEach(({ node }) => {
      if (
        (node.metaObject as OnlineNotification['metaObject'])?.id === id ||
        (node as OnlineNotification)?.id === id
      ) {
        node.seen = true
      }
    })

    cache.writeQuery({
      ...queryOptions,
      data: {
        onlineNotifications: {
          ...clonedQueryCache.onlineNotifications,
        },
      },
    })

    return () => {
      cache.writeQuery({
        ...queryOptions,
        data: oldQueryCache,
      })
    }
  }

  const seenNotificationMutation = new MutationHandler(useOnlineNotificationSeenMutation(), {
    errorNotificationMessage: __('The online notification could not be marked as seen.'),
  })

  const seenNotification = async (id: Scalars['ID']['output']) => {
    const revertCache = updateSeenNotificationCache(id)

    return seenNotificationMutation.send({ objectId: id }).catch(() => revertCache?.())
  }

  const markAllSeenMutation = new MutationHandler(useOnlineNotificationMarkAllAsSeenMutation(), {
    errorNotificationMessage: __('Cannot set online notifications as seen'),
  })

  const markAllRead = async () => {
    const revertCache = updateAllSeenNotificationCache()

    return markAllSeenMutation.send().catch(() => revertCache?.())
  }

  const deleteNotificationMutation = new MutationHandler(useOnlineNotificationDeleteMutation())

  const deleteNotification = async (id: Scalars['ID']['output']) => {
    const revertCache = removeNotificationCacheUpdate(id)

    return deleteNotificationMutation
      .send({
        onlineNotificationId: id,
      })
      .catch(() => revertCache?.())
  }

  const deleteAllMutation = new MutationHandler(useOnlineNotificationDeleteAllMutation(), {
    errorNotificationMessage: __('Online notifications could not be cleared.'),
  })

  const deleteAllNotifications = async () => {
    return deleteAllMutation
      .send({})
      .then(() => {
        cache.writeQuery({
          query: OnlineNotificationsDocument,
          data: {
            onlineNotifications: {
              __typename: 'OnlineNotificationConnection',
              edges: [],
              pageInfo: { __typename: 'PageInfo', endCursor: null, hasNextPage: false },
            },
          },
        })

        return true
      })
      .catch(() => false)
  }

  return {
    seenNotification,
    deleteNotification,
    deleteNotificationMutation,
    markAllRead,
    deleteAllNotifications,
  }
}
