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

import { nullableMock } from '#tests/support/utils.ts'

import type { OnlineNotificationsQuery, Scalars } from '#shared/graphql/types.ts'
import type { DeepPartial } from '#shared/types/utils.ts'

import type { LastArrayElement } from 'type-fest'

type OnlineNotificationNode = NonNullable<
  LastArrayElement<OnlineNotificationsQuery['onlineNotifications']['edges']>
>['node']

export const mockOnlineNotification = (
  id: Scalars['ID']['output'],
  mockNotification: DeepPartial<OnlineNotificationNode>,
): OnlineNotificationNode => {
  return nullableMock<OnlineNotificationNode>({
    __typename: 'OnlineNotification',
    seen: false,
    createdAt: new Date().toISOString(),
    createdBy: {
      __typename: 'User',
      id: '123',
      fullname: 'Full Name',
      lastname: 'Name',
      firstname: 'Full',
      email: 'email@example.org',
      vip: false,
      outOfOffice: false,
      outOfOfficeStartAt: null,
      outOfOfficeEndAt: null,
      active: true,
      image: null,
    },
    typeName: 'update',
    objectName: 'Ticket',
    meta: {
      __typename: 'OnlineNotificationMeta',
      createdByAi: false,
    },
    metaObject: {
      __typename: 'Ticket',
      id: '111',
      internalId: 111,
      title: 'Ticket Title',
    },
    id,
    ...mockNotification,
  })
}

export const mockOnlineNotificationQuery = (
  mockNotifications: Array<DeepPartial<OnlineNotificationNode>>,
): OnlineNotificationsQuery => {
  const edges = mockNotifications.map((item, index) => {
    const id = index + 1

    return {
      cursor: `node${id}`,
      node: mockOnlineNotification(id.toString(), item),
    }
  })

  return nullableMock<OnlineNotificationsQuery>({
    onlineNotifications: {
      edges,
      pageInfo: {
        endCursor: edges.at(-1)?.cursor || null,
        hasNextPage: false,
      },
    },
  })
}
