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

import { ApolloClient, ApolloLink, execute, gql, InMemoryCache, Observable } from '@apollo/client'
import { createPinia, setActivePinia } from 'pinia'

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

import { useNotifications } from '#shared/components/CommonNotifications/index.ts'
import { LogoutDocument } from '#shared/graphql/mutations/logout.api.ts'
import { ApplicationConfigDocument } from '#shared/graphql/queries/applicationConfig.api.ts'
import { ConfigUpdatesDocument } from '#shared/graphql/subscriptions/configUpdates.api.ts'
import trackOperationsLink, {
  cancelActiveOperations,
} from '#shared/server/apollo/link/trackOperations.ts'
import {
  authenticationInvalidated,
  setAuthenticationInvalidated,
} from '#shared/server/apollo/utils/authenticationState.ts'

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

vi.mock('#shared/server/apollo/client.ts', () => {
  return {
    clearApolloClientStore: () => Promise.resolve(),
  }
})

const queryDocument = gql`
  query sample {
    sample {
      id
    }
  }
`

const subscriptionDocument = gql`
  subscription sampleUpdates {
    sampleUpdates {
      id
    }
  }
`

const mockUnauthenticatedConfig = () => {
  mockGraphQLApi(ApplicationConfigDocument).willResolve({
    applicationConfig: [{ key: 'product_name', value: 'Zammad' }],
  })
  mockGraphQLSubscription(ConfigUpdatesDocument)
}

const authenticateStore = () => {
  const authentication = useAuthenticationStore()
  const session = useSessionStore()

  authentication.authenticated = true
  session.id = '123456789'

  return authentication
}

// Apollo Client 4 executes a link only on behalf of a client.
const executionContext = {
  client: new ApolloClient({ cache: new InMemoryCache(), link: ApolloLink.empty() }),
}

// Starts a subscription through the real tracking link, so that the logout has
//  something to cancel. Its teardown is what unsubscribes the Action Cable
//  channel in the application.
const startTrackedSubscription = () => {
  const teardownSpy = vi.fn()

  const terminatingLink = new ApolloLink(() => new Observable(() => teardownSpy))

  execute(
    ApolloLink.from([trackOperationsLink, terminatingLink]),
    { query: subscriptionDocument },
    executionContext,
  ).subscribe(() => {})

  return teardownSpy
}

// Starts a query through the real tracking link, so that the logout has
//  something to cancel. Its teardown is what aborts the HTTP request in the
//  application.
const startTrackedQuery = () => {
  const teardownSpy = vi.fn()

  const terminatingLink = new ApolloLink(() => new Observable(() => teardownSpy))

  execute(
    ApolloLink.from([trackOperationsLink, terminatingLink]),
    { query: queryDocument },
    executionContext,
  ).subscribe(() => {})

  return teardownSpy
}

describe('Authentication Store', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    mockUnauthenticatedConfig()
    useNotifications().clearAllNotifications()
  })

  afterEach(() => {
    // Make sure no state of a previous example is left behind.
    cancelActiveOperations()
    setAuthenticationInvalidated(false)
  })

  it('clears the authentication after a logout', async () => {
    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: null,
      },
    })

    const authentication = authenticateStore()

    await authentication.logout()

    expect(authentication.authenticated).toBe(false)
    expect(useSessionStore().id).toBe(null)
  })

  it('pauses the automatic third-party login redirect after a logout', async () => {
    localStorage.removeItem('third-party-login-auto-redirect-paused')

    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: null,
      },
    })

    await authenticateStore().logout()

    expect(localStorage.getItem('third-party-login-auto-redirect-paused')).toBe('true')
  })

  it('finishes the logout when the redirect pause cannot be stored', async () => {
    const { setItem } = Storage.prototype

    const setItemSpy = vi
      .spyOn(Storage.prototype, 'setItem')
      .mockImplementation(function setItemOrThrow(this: Storage, key: string, value: string) {
        if (key === 'third-party-login-auto-redirect-paused')
          throw new DOMException('Quota exceeded', 'QuotaExceededError')

        setItem.call(this, key, value)
      })

    onTestFinished(() => setItemSpy.mockRestore())

    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: null,
      },
    })

    const authentication = authenticateStore()

    await authentication.logout()

    expect(authentication.authenticated).toBe(false)
    expect(useSessionStore().id).toBe(null)
  })

  it('keeps the automatic third-party login redirect after a logout at the identity provider', async () => {
    localStorage.removeItem('third-party-login-auto-redirect-paused')

    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: 'https://idp.example.com/logout',
      },
    })

    await authenticateStore().logout()

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

  it('cancels the running subscriptions during a logout', async () => {
    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: null,
      },
    })

    const authentication = authenticateStore()
    const teardownSpy = startTrackedSubscription()

    await authentication.logout()

    expect(teardownSpy).toHaveBeenCalledOnce()
    expect(authenticationInvalidated()).toBe(true)
  })

  it('cancels the running subscriptions when the authentication is cleared', async () => {
    const authentication = authenticateStore()
    const teardownSpy = startTrackedSubscription()

    await authentication.clearAuthentication()

    expect(teardownSpy).toHaveBeenCalledOnce()
    expect(authenticationInvalidated()).toBe(true)
  })

  it('cancels the running queries during a logout', async () => {
    mockGraphQLApi(LogoutDocument).willResolve({
      logout: {
        success: true,
        errors: null,
        externalLogoutUrl: null,
      },
    })

    const authentication = authenticateStore()
    const teardownSpy = startTrackedQuery()

    await authentication.logout()

    expect(teardownSpy).toHaveBeenCalledOnce()
  })

  it('cancels the running queries when the authentication is cleared', async () => {
    const authentication = authenticateStore()
    const teardownSpy = startTrackedQuery()

    await authentication.clearAuthentication()

    expect(teardownSpy).toHaveBeenCalledOnce()
  })

  it('clears the authentication even when the logout mutation fails', async () => {
    mockGraphQLApi(LogoutDocument).willFailWithNetworkError(new Error('Network error'))

    const authentication = authenticateStore()

    await expect(authentication.logout()).resolves.not.toThrow()

    // The client side of the session cannot be revived, so the user must not be
    //  left behind as authenticated.
    expect(authentication.authenticated).toBe(false)
    expect(useSessionStore().id).toBe(null)

    // The logout continues in any case, so its failure is nothing the user
    //  could act on.
    expect(useNotifications().notifications.value).toEqual([])
  })
})
