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

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

import { visitView } from '#tests/support/components/visitView.ts'
import { mockApplicationConfig } from '#tests/support/mock-applicationConfig.ts'
import { mockPermissions } from '#tests/support/mock-permissions.ts'

import { mockUserQuery } from '#shared/entities/user/graphql/queries/user.mocks.ts'
import type { User } from '#shared/graphql/types.ts'

import { mockUserHistoryQuery } from '#desktop/entities/user/graphql/queries/history.mocks.ts'

const user: User = {
  __typename: 'User',
  id: 'gid://zammad/User/2',
  internalId: 2,
  firstname: 'Nicole',
  lastname: 'Braun',
  fullname: 'Nicole Braun',
  email: 'nicole.braun@zammad.org',
  organization: {
    __typename: 'Organization',
    id: 'gid://zammad/Organization/1',
    internalId: 1,
    name: 'Zammad Foundation',
    active: true,
    policy: {
      update: true,
      destroy: true,
    },
    createdAt: '2020-01-01T12:00:00Z',
    updatedAt: '2020-01-01T12:00:00Z',
  },
  phone: '+49 123 4567890',
  mobile: '+49 987 6543210',
  image: null,
  vip: false,
  outOfOffice: false,
  outOfOfficeStartAt: null,
  outOfOfficeEndAt: null,
  hasSecondaryOrganizations: false,
  active: true,
  policy: {
    update: true,
    destroy: true,
  },
  ticketsCount: {
    __typename: 'TicketCount',
    open: 5,
    closed: 10,
    organizationOpen: 15,
    organizationClosed: 20,
  },
  createdAt: '2020-01-01T12:00:00Z',
  updatedAt: '2020-01-01T12:00:00Z',
}

describe('User Detail View - History Flyout', () => {
  beforeEach(() => {
    mockApplicationConfig({
      fqdn: 'zammad.example.com',
      http_type: 'http',
    })

    mockPermissions(['ticket.agent'])
    mockUserQuery({ user })
  })

  it('opens the History flyout from the action menu', async () => {
    mockUserHistoryQuery({ userHistory: [] })

    const view = await visitView(`/users/${user.internalId}`)

    const main = view.getByRole('main')
    const header = within(main).getByTestId('user-detail-top-bar-full-details')

    await view.events.click(within(header).getByRole('button', { name: 'Additional actions' }))

    const actionPopover = await view.findByRole('region', { name: 'Additional actions' })
    await view.events.click(within(actionPopover).getByRole('button', { name: 'History' }))

    expect(await view.findByRole('heading', { name: 'History', level: 2 })).toBeInTheDocument()
  })

  it('renders user history entries inside the flyout', async () => {
    mockUserHistoryQuery({
      userHistory: [
        {
          __typename: 'HistoryGroup',
          createdAt: '2021-09-29T14:00:00Z',
          records: [
            {
              __typename: 'HistoryRecord',
              events: [
                {
                  __typename: 'HistoryRecordEvent',
                  action: 'created',
                  createdAt: '2021-09-29T14:00:00Z',
                  object: {
                    __typename: 'User',
                    id: user.id,
                    fullname: user.fullname,
                  },
                },
              ],
              issuer: {
                __typename: 'User',
                id: 'gid://zammad/User/3',
                internalId: 3,
                firstname: 'John',
                lastname: 'Doe',
                fullname: 'John Doe',
              },
            },
          ],
        },
      ],
    })

    const view = await visitView('/users/2')

    const main = view.getByRole('main')
    const header = within(main).getByTestId('user-detail-top-bar-full-details')

    await view.events.click(within(header).getByRole('button', { name: 'Additional actions' }))
    const actionPopover = await view.findByRole('region', { name: 'Additional actions' })
    await view.events.click(within(actionPopover).getByRole('button', { name: 'History' }))

    expect(await view.findByText('Created')).toBeInTheDocument()
    expect(view.getByText('John Doe')).toBeInTheDocument()
    expect(view.getByText('2021-09-29 14:00')).toBeInTheDocument()
  })
})
