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

import { createPinia, setActivePinia } from 'pinia'

import { mockGraphQLApi, mockGraphQLSubscription } from '#tests/support/mock-graphql-api.ts'

import { CurrentUserDocument } from '#shared/graphql/queries/currentUser.api.ts'
import { SessionDocument } from '#shared/graphql/queries/session.api.ts'
import { CurrentUserUpdatesDocument } from '#shared/graphql/subscriptions/currentUserUpdates.api.ts'

import { useSessionStore } from '../session.ts'

const userData = {
  __typename: 'User',
  id: '123456789',
  internalId: 1,
  firstname: 'John',
  lastname: 'Doe',
  fullname: 'John Doe',
  hasContentTranslationAutoAvailable: false,
  email: 'zammad@example.com',
  image: 'c2715a3e92c7e375b8c212d25d431e2a',
  preferences: {
    locale: 'de-de',
  },
  personalSettings: {
    __typename: 'UserPersonalSettings',
    callerNotificationEnabled: false,
    notificationConfig: {
      __typename: 'UserPersonalSettingsNotificationConfig',
      groupIds: [1],
      matrix: {
        __typename: 'UserPersonalSettingsNotificationMatrix',
        create: {
          __typename: 'UserPersonalSettingsNotificationMatrixRow',
          channel: {
            __typename: 'UserPersonalSettingsNotificationMatrixChannel',
            email: true,
            online: true,
          },
          criteria: {
            __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
            no: false,
            ownedByMe: true,
            ownedByNobody: true,
            subscribed: true,
          },
        },
        escalation: {
          __typename: 'UserPersonalSettingsNotificationMatrixRow',
          channel: {
            __typename: 'UserPersonalSettingsNotificationMatrixChannel',
            email: true,
            online: true,
          },
          criteria: {
            __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
            no: false,
            ownedByMe: true,
            ownedByNobody: true,
            subscribed: true,
          },
        },
        reminderReached: {
          __typename: 'UserPersonalSettingsNotificationMatrixRow',
          channel: {
            __typename: 'UserPersonalSettingsNotificationMatrixChannel',
            email: true,
            online: true,
          },
          criteria: {
            __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
            no: false,
            ownedByMe: true,
            ownedByNobody: true,
            subscribed: true,
          },
        },
        update: {
          __typename: 'UserPersonalSettingsNotificationMatrixRow',
          channel: {
            __typename: 'UserPersonalSettingsNotificationMatrixChannel',
            email: true,
            online: true,
          },
          criteria: {
            __typename: 'UserPersonalSettingsNotificationMatrixCriteria',
            no: false,
            ownedByMe: true,
            ownedByNobody: true,
            subscribed: true,
          },
        },
      },
    },
    notificationSound: {
      __typename: 'UserPersonalSettingsNotificationSound',
      enabled: true,
      file: 'Ring',
    },
  },
  objectAttributeValues: [],
  authorizations: [],
  organization: {
    __typename: 'Organization',
    id: '234241',
    internalId: 1,
    name: 'Zammad Foundation',
    objectAttributeValues: [],
    active: true,
  },
  permissions: {
    __typename: 'Permission',
    names: ['admin'],
  },
  hasBetaUiSwitchAvailable: false,
  hasSecondaryOrganizations: false,
  outOfOffice: false,
  outOfOfficeStartAt: null,
  outOfOfficeEndAt: null,
  outOfOfficeReplacement: null,
}

describe('Session Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('is empty by default', () => {
    const session = useSessionStore()

    expect(session.id).toBe(null)
    expect(session.user).toBe(null)
  })

  it('get current user and check on user update subscription', async () => {
    const session = useSessionStore()

    mockGraphQLApi(CurrentUserDocument).willResolve({
      currentUser: userData,
    })
    const userUpdateSubscription = mockGraphQLSubscription(CurrentUserUpdatesDocument)

    await session.getCurrentUser()

    expect(session.user).toEqual(userData)

    const updatedUserData = {
      ...userData,
      firstname: 'Jane',
      lastname: 'Doe',
      fullname: 'Jane Doe',
    }

    await userUpdateSubscription.next({
      data: {
        userUpdates: {
          __typename: 'UserUpdatesPayload',
          user: updatedUserData,
        },
      },
    })

    expect(session.user).toEqual(updatedUserData)
  })

  it('updates the user after a frozen subscription result', async () => {
    const session = useSessionStore()

    mockGraphQLApi(CurrentUserDocument).willResolve({
      currentUser: userData,
    })
    const userUpdateSubscription = mockGraphQLSubscription(CurrentUserUpdatesDocument)

    await session.getCurrentUser()

    await userUpdateSubscription.next({
      data: {
        userUpdates: {
          __typename: 'UserUpdatesPayload',
          user: { ...userData, firstname: 'Jane' },
        },
      },
    })

    expect(Object.isFrozen(session.user)).toBe(true)

    session.setUserPreference('theme', 'dark')
    session.updateUser({ image: 'new-image-hash' })

    expect(session.user).toMatchObject({
      firstname: 'Jane',
      image: 'new-image-hash',
      preferences: { locale: 'de-de', theme: 'dark' },
    })
  })

  it('resumes the automatic third-party login redirect for an existing session', async () => {
    localStorage.setItem('third-party-login-auto-redirect-paused', 'true')
    sessionStorage.setItem('third-party-login-auto-redirect-at', String(Date.now()))

    mockGraphQLApi(SessionDocument).willResolve({
      session: { __typename: 'Session', id: 'session-id', afterAuth: null },
    })

    await useSessionStore().checkSession()

    expect(localStorage.getItem('third-party-login-auto-redirect-paused')).toBeNull()
    expect(sessionStorage.getItem('third-party-login-auto-redirect-at')).toBeNull()
  })
})
