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

import { within } from '@testing-library/vue'
import { ref } from 'vue'

import { getByIconName } from '#tests/support/components/iconQueries.ts'
import renderComponent from '#tests/support/components/renderComponent.ts'
import { mockPermissions } from '#tests/support/mock-permissions.ts'
import { waitForNextTick } from '#tests/support/utils.ts'

import type { TicketLiveAppUser } from '#shared/entities/ticket/types.ts'
import { createDummyTicket } from '#shared/entities/ticket-article/__tests__/mocks/ticket.ts'
import { mockMacrosQuery, waitForMacrosQueryCalls } from '#shared/graphql/queries/macros.mocks.ts'
import { getMacrosUpdateSubscriptionHandler } from '#shared/graphql/subscriptions/macrosUpdate.mocks.ts'
import { convertToGraphQLId } from '#shared/graphql/utils.ts'

import TicketDetailBottomBar, {
  type Props,
} from '#desktop/pages/ticket/components/TicketDetailView/TicketDetailBottomBar/TicketDetailBottomBar.vue'

import liveUserList from './mocks/live-user-list.json'

const ticket = createDummyTicket()

vi.mock('#desktop/pages/ticket/composables/useTicketInformation.ts', () => ({
  useTicketInformation: () => ({
    ticket: ref(ticket),
  }),
}))

const renderTicketDetailBottomBar = (props?: Partial<Props>) =>
  renderComponent(TicketDetailBottomBar, {
    props: {
      disabled: false,
      dirty: false,
      isTicketEditable: true,
      groupId: convertToGraphQLId('Group', 2),
      liveUserList: [],
      ticketId: ticket.id,
      isTicketAgent: true,
      ...props,
    },
    store: true,
    router: true,
  })

describe('TicketDetailBottomBar', () => {
  it('renders submit button if form node id is provided', () => {
    const wrapper = renderTicketDetailBottomBar()

    expect(wrapper.getByRole('button', { name: 'Update' })).toBeInTheDocument()
  })

  it('highlights the update action for agents only when the form is dirty', async () => {
    const wrapper = renderTicketDetailBottomBar()

    expect(wrapper.getByRole('button', { name: 'Update' })).toHaveClass('bg-green-200')
    expect(wrapper.getByRole('button', { name: 'Drafts & macros' })).toHaveClass('bg-green-200')

    await wrapper.rerender({
      dirty: true,
    })

    expect(wrapper.getByRole('button', { name: 'Update' })).toHaveClass('bg-yellow-300')
    expect(wrapper.getByRole('button', { name: 'Drafts & macros' })).toHaveClass('bg-yellow-300')

    await wrapper.rerender({
      dirty: false,
    })

    expect(wrapper.getByRole('button', { name: 'Update' })).toHaveClass('bg-green-200')
    expect(wrapper.getByRole('button', { name: 'Drafts & macros' })).toHaveClass('bg-green-200')
  })

  it('highlights the update action for customers only when the form is dirty', async () => {
    const wrapper = renderTicketDetailBottomBar({
      isTicketAgent: false,
    })

    expect(wrapper.queryByRole('button', { name: 'Drafts & macros' })).not.toBeInTheDocument()
    expect(wrapper.getByRole('button', { name: 'Update' })).toHaveClass('bg-green-200')

    await wrapper.rerender({
      dirty: true,
    })

    expect(wrapper.getByRole('button', { name: 'Update' })).toHaveClass('bg-yellow-300')
  })

  it('renders discard unsaved changes button if dirty prop is true', async () => {
    const wrapper = renderTicketDetailBottomBar({
      dirty: true,
    })

    expect(
      wrapper.queryByRole('button', { name: 'Discard your unsaved changes' }),
    ).toBeInTheDocument()

    await wrapper.rerender({
      dirty: false,
    })

    expect(
      wrapper.queryByRole('button', { name: 'Discard your unsaved changes' }),
    ).not.toBeInTheDocument()
  })

  it('should disable buttons if disabled prop is true', () => {
    const wrapper = renderTicketDetailBottomBar({
      dirty: true,
      disabled: true,
    })

    expect(wrapper.getByRole('button', { name: 'Update' })).toBeDisabled()

    expect(wrapper.queryByRole('button', { name: 'Discard your unsaved changes' })).toBeDisabled()
  })

  it.each(['submit', 'discard'])('emits %s event when button is clicked', async (eventName) => {
    const wrapper = renderTicketDetailBottomBar({
      dirty: true,
      disabled: false,
    })

    if (eventName === 'submit') {
      await wrapper.events.click(wrapper.getByRole('button', { name: 'Update' }))

      expect(wrapper.emitted('submit')).toBeTruthy()
    }

    if (eventName === 'discard') {
      const discardButton = wrapper.getByRole('button', { name: 'Discard your unsaved changes' })

      // Discarding asks first, so the button is not red.
      expect(discardButton).toHaveClass('bg-green-200')
      expect(discardButton).not.toHaveClass('bg-pink-100')
      expect(getByIconName(discardButton, 'trash3')).toBeInTheDocument()

      await wrapper.events.click(discardButton)

      expect(wrapper.emitted('discard')).toBeTruthy()
    }
  })

  describe('Drafts', () => {
    it('should not display draft information if ticket has no draft', () => {
      const wrapper = renderTicketDetailBottomBar({
        hasAvailableDraft: false,
      })

      expect(wrapper.queryByRole('button', { name: 'Draft available' })).not.toBeInTheDocument()
    })

    it('should display draft information if ticket has a draft', () => {
      const wrapper = renderTicketDetailBottomBar({
        hasAvailableDraft: true,
      })

      expect(wrapper.getByRole('button', { name: 'Draft available' })).toBeInTheDocument()
    })
  })

  describe('Macros', () => {
    it('displays action menu button for macros for agent with update permission', async () => {
      mockPermissions(['ticket.agent'])

      mockMacrosQuery({
        macros: [
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 1),
            active: true,
            name: 'Macro 1',
            uxFlowNextUp: 'next_task',
          },
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 2),
            active: true,
            name: 'Macro 2',
            uxFlowNextUp: 'next_task',
          },
        ],
      })

      const wrapper = renderTicketDetailBottomBar()

      const actionMenu = await wrapper.findByLabelText('Drafts & macros')

      await wrapper.events.click(actionMenu)

      const menu = await wrapper.findByRole('menu')

      expect(menu).toBeInTheDocument()
      expect(within(menu).getByText('Macros')).toBeInTheDocument()
      expect(within(menu).getByText('Macro 1')).toBeInTheDocument()
      expect(within(menu).getByText('Macro 2')).toBeInTheDocument()
    })

    it('hides action menu, submit and cancel buttons for agent without update permission', async () => {
      const wrapper = renderTicketDetailBottomBar({
        isTicketEditable: false,
      })

      expect(wrapper.queryByRole('button', { name: 'Update' })).not.toBeInTheDocument()

      expect(
        wrapper.queryByRole('button', { name: 'Discard your unsaved changes' }),
      ).not.toBeInTheDocument()

      expect(wrapper.queryByLabelText('Drafts & macros')).not.toBeInTheDocument()
    })

    it('reloads macro query if subscription is triggered', async () => {
      mockMacrosQuery({
        macros: [],
      })

      renderTicketDetailBottomBar()

      const calls = await waitForMacrosQueryCalls()

      expect(calls?.at(-1)?.variables).toEqual({
        selector: {
          entityIds: [convertToGraphQLId('Group', 2)],
        },
      })

      await waitForNextTick()

      await getMacrosUpdateSubscriptionHandler().trigger({
        macrosUpdate: {
          macroId: convertToGraphQLId('Macro', 1),
          groupIds: [],
          removeMacroId: null,
        },
      })

      mockMacrosQuery({
        macros: [
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 1),
            active: true,
            name: 'Macro 1',
            uxFlowNextUp: 'next_task',
          },
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 2),
            active: true,
            name: 'Macro updated',
            uxFlowNextUp: 'next_task',
          },
        ],
      })

      expect(calls.length).toBe(2)
    })

    it('submits event if macro is clicked', async () => {
      mockPermissions(['ticket.agent'])

      mockMacrosQuery({
        macros: [
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 1),
            active: true,
            name: 'Macro 1',
            uxFlowNextUp: 'next_task',
          },
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 2),
            active: true,
            name: 'Macro 2',
            uxFlowNextUp: 'next_task',
          },
        ],
      })

      const wrapper = renderTicketDetailBottomBar()

      const actionMenu = await wrapper.findByLabelText('Drafts & macros')

      await wrapper.events.click(actionMenu)

      const menu = await wrapper.findByRole('menu')

      expect(within(menu).getByRole('menuitem')).toBeInTheDocument()

      await wrapper.events.click(within(menu).getByText('Macro 1'))

      expect(wrapper.emitted('execute-macro')).toEqual([
        [
          {
            __typename: 'Macro',
            id: convertToGraphQLId('Macro', 1),
            internalId: 1,
            active: true,
            name: 'Macro 1',
            uxFlowNextUp: 'next_task',
          },
        ],
      ])
    })

    it('hides macros if there is no group id', async () => {
      const wrapper = renderTicketDetailBottomBar({
        groupId: undefined,
      })

      const addonButton = wrapper.getByRole('button', {
        name: 'Drafts & macros',
      })

      await wrapper.events.click(addonButton)

      expect(within(wrapper.getByRole('menu')).queryByRole('menuitem')).not.toBeInTheDocument()
    })

    it('keeps the action menu with an empty state if there are no macros and drafts cannot be used', async () => {
      mockMacrosQuery({
        macros: [],
      })

      const wrapper = renderTicketDetailBottomBar({
        canUseDraft: false,
      })

      await waitForMacrosQueryCalls()

      const addonButton = wrapper.getByRole('button', {
        name: 'Drafts & macros',
      })

      await wrapper.events.click(addonButton)

      const menu = wrapper.getByRole('menu')

      expect(within(menu).queryByRole('menuitem')).not.toBeInTheDocument()
      expect(within(menu).getByText('No items available')).toBeInTheDocument()
    })
  })

  describe('Live Users', () => {
    it('shows live user information as avatars', async () => {
      const wrapper = renderTicketDetailBottomBar({
        liveUserList: liveUserList as TicketLiveAppUser[],
      })

      expect(wrapper.getByRole('img', { name: 'Avatar (Nicole Braun) (VIP)' })).toBeInTheDocument()

      expect(wrapper.getByRole('img', { name: 'Avatar (Test Admin Agent)' })).toBeInTheDocument()

      expect(wrapper.getByRole('img', { name: 'Avatar (Agent 1 Test)' })).toBeInTheDocument()

      expect(wrapper.getByRole('img', { name: 'Avatar (Agent 2 Test)' })).toBeInTheDocument()
    })
  })
})
