/**
 * (navigation, subscribe mutation payload, pending UI).
 */
import React from 'react'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import ApplicationCard from '../index'
import { MOCK_APPLICATION_DETAIL } from '@/_tests_/mocks'

const mockPush = jest.fn()
jest.mock('@/i18n/navigation', () => ({
  useRouter: () => ({ push: mockPush }),
}))
jest.mock('next-intl', () => ({ useTranslations: () => (k: string) => k }))
const mockMutate = jest.fn()
const mockUseSubscribeApiMutation = jest.fn()
jest.mock('@/services/application/application.mutations', () => ({
  useSubscribeApiMutation: () => mockUseSubscribeApiMutation(),
}))

const sandboxApp = {
  ...MOCK_APPLICATION_DETAIL,
  tier: 'SANDBOX' as const,
}

const cardPropsBase = {
  api_id: 'api-1',
  hasAnySubscribed: false,
  api_product_name: 'Test API',
}

describe('[components/application/listing/card] ApplicationCard', () => {
  beforeEach(() => {
    mockPush.mockClear()
    mockMutate.mockClear()
    mockUseSubscribeApiMutation.mockReturnValue({
      mutate: mockMutate,
      isPending: false,
    })
  })

  describe('render', () => {
    it('shows application display name', () => {
      render(
        <ApplicationCard
          application={sandboxApp as never}
          {...cardPropsBase}
        />
      )
      expect(screen.getByText(MOCK_APPLICATION_DETAIL.name)).toBeInTheDocument()
    })
  })

  describe('sandbox actions', () => {
    it('test sandbox then detail navigates tryout then application overview', async () => {
      render(
        <ApplicationCard
          application={{ ...sandboxApp, registeredThisApi: true } as never}
          {...cardPropsBase}
        />
      )

      await userEvent.click(screen.getByText('btn.test_sandbox'))
      await userEvent.click(screen.getByText('btn.detail'))

      expect(mockPush).toHaveBeenNthCalledWith(
        1,
        '/api-products/api-1/tryout?app_id=app-123'
      )
      expect(mockPush).toHaveBeenNthCalledWith(
        2,
        '/api-products/api-1/application/app-123?api_product_name=Test API'
      )
    })
  })

  describe('subscribe flow', () => {
    it('confirm subscribe calls mutate with body contract', async () => {
      render(
        <ApplicationCard
          application={{ ...sandboxApp, subscribedApiIds: [] } as never}
          {...cardPropsBase}
        />
      )

      await userEvent.click(screen.getByText('btn.subscribe'))
      await userEvent.click(screen.getByText('btn.continue'))

      expect(mockMutate).toHaveBeenCalledWith({
        applicationId: 'app-123',
        body: { apiId: 'api-1', throttlingPolicy: '10PerMin' },
      })
    })

    it('shows loading copy when mutation pending', () => {
      mockUseSubscribeApiMutation.mockReturnValue({
        mutate: mockMutate,
        isPending: true,
      })

      render(
        <ApplicationCard
          application={{ ...sandboxApp, subscribedApiIds: [] } as never}
          {...cardPropsBase}
        />
      )

      expect(screen.getByText('mess.subscribe.loading')).toBeInTheDocument()
    })
  })
})
