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

import { computed, ref } from 'vue'

import { renderComponent } from '#tests/support/components/index.ts'
import { mockPermissions } from '#tests/support/mock-permissions.ts'
import { mockRouterHooks } from '#tests/support/mock-vue-router.ts'
import { waitForNextTick } from '#tests/support/utils.ts'

import type { TicketById } from '#shared/entities/ticket/types.ts'
import { createDummyTicket } from '#shared/entities/ticket-article/__tests__/mocks/ticket.ts'
import { convertToGraphQLId } from '#shared/graphql/utils.ts'

import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
import { TICKET_KEY } from '#desktop/pages/ticket/composables/useTicketInformation.ts'

import { TicketSidebarScreenType } from '../../../../types/sidebar.ts'
import customerSidebarPlugin from '../../plugins/customer.ts'
import TicketSidebarCustomerContent from '../TicketSidebarCustomerContent.vue'

const secondaryOrganizations = {
  array: [
    {
      __typename: 'Organization',
      id: convertToGraphQLId('Organization', 2),
      name: 'Zammad Org',
      internalId: 2,
      active: true,
    },
    {
      __typename: 'Organization',
      id: convertToGraphQLId('Organization', 3),
      name: 'Zammad Inc',
      internalId: 3,
      active: true,
    },
    {
      __typename: 'Organization',
      id: convertToGraphQLId('Organization', 4),
      name: 'Zammad Ltd',
      internalId: 4,
      active: true,
    },
  ],
  totalCount: 5,
}

const mockedUser = {
  __typename: 'User',
  id: convertToGraphQLId('User', 2),
  internalId: 2,
  firstname: 'Nicole',
  lastname: 'Braun',
  fullname: 'Nicole Braun',
  email: 'nicole.braun@zammad.org',
  organization: {
    __typename: 'Organization',
    id: convertToGraphQLId('Organization', 1),
    internalId: 1,
    name: 'Zammad Foundation',
    ticketsCount: null,
    active: true,
  },
  vip: false,
  ticketsCount: {
    open: 42,
    closed: 10,
  },
  policy: {
    update: true,
  },
}
const defaultTicket = createDummyTicket()

// The sidebar owns the item; here it stands in with the ticket's update policy as its condition.
const changeCustomerMenuItem = (ticket: TicketById): MenuItem => ({
  key: 'ticket-change-customer',
  label: 'Change customer',
  icon: 'user',
  show: () => ticket.policy.update,
  onClick: vi.fn(),
})

mockRouterHooks()

const renderTicketSidebarCustomerContent = async (
  screen: TicketSidebarScreenType = TicketSidebarScreenType.TicketCreate,
  ticket = defaultTicket,
  options: any = {
    user: null,
  },
) =>
  renderComponent(TicketSidebarCustomerContent, {
    props: {
      modelValue: {},
      sidebarPlugin: customerSidebarPlugin,
      customer: options.user || mockedUser,
      secondaryOrganizations,
      entityAttributes: [
        {
          name: 'email',
          display: 'Email',
          dataType: 'input',
          dataOption: {
            type: 'email',
            maxlength: 150,
            null: true,
            item_class: 'formGroup--halfSize',
          },
          isInternal: true,
        },
      ],
      context: {
        screenType: screen,
      },
      changeCustomerMenuItem: changeCustomerMenuItem(ticket),
    },
    provide: [
      [
        TICKET_KEY,
        {
          ticketId: computed(() => ticket.id),
          ticket: computed(() => ticket),
          form: ref(),
          showTicketArticleReplyForm: () => {},
          isTicketEditable: computed(() => true),
          newTicketArticlePresent: ref(false),
          ticketInternalId: computed(() => ticket.internalId),
        },
      ],
    ],
    router: true,
    ...options,
  })

describe('TicketSidebarCustomerContent.vue', () => {
  beforeEach(() => {
    mockPermissions(['ticket.agent'])
  })

  describe('ticket-create-screen', () => {
    it('renders customer info', async () => {
      const wrapper = await renderTicketSidebarCustomerContent()

      await waitForNextTick()

      expect(wrapper.getByRole('heading', { level: 2 })).toHaveTextContent('Customer')

      expect(wrapper.queryByRole('button', { name: 'Action menu button' })).toBeInTheDocument()

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

      expect(wrapper.getByText('Nicole Braun')).toBeInTheDocument()

      expect(wrapper.getByRole('link', { name: 'Zammad Foundation' })).toHaveAttribute(
        'href',
        '/organizations/1',
      )

      expect(wrapper.getByText('Email')).toBeInTheDocument()

      expect(wrapper.getByRole('link', { name: 'nicole.braun@zammad.org' })).toBeInTheDocument()

      expect(wrapper.getByText('Secondary organizations')).toBeInTheDocument()

      expect(
        await wrapper.findByRole('link', {
          name: 'Avatar (Zammad Org)Zammad Org',
        }),
      ).toHaveAttribute('href', '/organizations/2')

      expect(wrapper.getByRole('link', { name: 'Avatar (Zammad Inc)Zammad Inc' })).toHaveAttribute(
        'href',
        '/organizations/3',
      )

      expect(wrapper.getByRole('link', { name: 'Avatar (Zammad Ltd)Zammad Ltd' })).toHaveAttribute(
        'href',
        '/organizations/4',
      )

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

      expect(wrapper.getByText('Tickets')).toBeInTheDocument()

      expect(wrapper.getByRole('link', { name: 'open tickets42' })).toBeInTheDocument()

      expect(wrapper.getByRole('link', { name: 'closed tickets10' })).toBeInTheDocument()
    })
  })

  describe('ticket-detail-screen', () => {
    it.each(['Change customer', 'Edit customer'])(
      'shows button for `%s` action',
      async (buttonLabel) => {
        const wrapper = await renderTicketSidebarCustomerContent(
          TicketSidebarScreenType.TicketDetailView,
        )

        await wrapper.events.click(
          wrapper.getByRole('button', {
            name: 'Action menu button',
          }),
        )

        expect(await wrapper.findByRole('button', { name: buttonLabel })).toBeInTheDocument()
      },
    )

    it('does not show `Change customer` action if user is agent and has no update permission', async () => {
      mockPermissions(['ticket.agent'])

      const wrapper = await renderTicketSidebarCustomerContent(
        TicketSidebarScreenType.TicketDetailView,
        {
          ...defaultTicket,
          policy: {
            __typename: 'PolicyTicket',
            update: false,
            agentReadAccess: true,
          },
        },
      )

      const actionMenuButton = wrapper.getByRole('button', { name: 'Action menu button' })

      await wrapper.events.click(actionMenuButton)

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

  it('does not show `Edit customer` when user has no update permission', async () => {
    mockPermissions(['ticket.agent'])

    const user = mockedUser
    mockedUser.policy.update = false
    const wrapper = await renderTicketSidebarCustomerContent(
      TicketSidebarScreenType.TicketDetailView,
      defaultTicket,
      user,
    )

    const actionMenuButton = wrapper.getByRole('button', { name: 'Action menu button' })

    await wrapper.events.click(actionMenuButton)

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