import { render, screen } from '@testing-library/react'
import type React from 'react'

import { getDefaultBillingPlanAvailability } from 'dbaas/screens/NextGen/my-tidb-creation/shared/billing-plan-availability'
import { useBillingPlanAvailability } from 'dbaas/screens/NextGen/my-tidb-creation/shared/use-billing-plan-availability'

import { TiDBNextGen } from './index'

jest.mock('mobx-react', () => ({
  observer: (component: unknown) => component
}))

jest.mock('@tidbcloud/uikit', () => ({
  Box: ({ children, sx }: React.PropsWithChildren<{ sx?: unknown }>) => {
    const style = typeof sx === 'function' ? sx({}) : sx

    if (style && typeof style === 'object' && 'display' in style && style.display === 'none') {
      return null
    }

    return <div>{children}</div>
  }
}))

jest.mock('dbaas/components/Placeholder', () => ({
  DelayedPageLoader: () => <div>loading</div>
}))

jest.mock('dbaas/screens/ClusterV2/components', () => ({
  ClusterPageShell: ({ children }: React.PropsWithChildren) => <div>{children}</div>
}))

jest.mock('dbaas/screens/NextGen/my-tidb-creation/shared/use-billing-plan-availability', () => ({
  useBillingPlanAvailability: jest.fn()
}))

jest.mock('./starter', () => ({
  Starter: () => <div>starter-page</div>
}))

jest.mock('./essential', () => ({
  Essential: () => <div>essential-page</div>
}))

const mockedUseBillingPlanAvailability = useBillingPlanAvailability as jest.MockedFunction<
  typeof useBillingPlanAvailability
>

const createPlanAvailability = (starterEnabled: boolean, essentialEnabled: boolean) => ({
  ...getDefaultBillingPlanAvailability(),
  Starter: { enabled: starterEnabled },
  Essential: { enabled: essentialEnabled }
})

describe('TiDBNextGen', () => {
  afterEach(() => {
    mockedUseBillingPlanAvailability.mockReset()
  })

  test('shows a loader while billing plan availability is loading', () => {
    mockedUseBillingPlanAvailability.mockReturnValue({
      planAvailability: createPlanAvailability(true, true),
      isLoading: true,
      isError: false
    })

    render(<TiDBNextGen />)

    expect(screen.getByText('loading')).toBeTruthy()
  })

  test('falls back to Essential when Starter is denied and Essential is allowed', () => {
    mockedUseBillingPlanAvailability.mockReturnValue({
      planAvailability: createPlanAvailability(false, true),
      isLoading: false,
      isError: false
    })

    render(<TiDBNextGen />)

    expect(screen.queryByText('starter-page')).toBeNull()
    expect(screen.getByText('essential-page')).toBeTruthy()
  })

  test('keeps Starter as the default view when Starter is allowed', () => {
    mockedUseBillingPlanAvailability.mockReturnValue({
      planAvailability: createPlanAvailability(true, true),
      isLoading: false,
      isError: false
    })

    render(<TiDBNextGen />)

    expect(screen.getByText('starter-page')).toBeTruthy()
    expect(screen.queryByText('essential-page')).toBeNull()
  })

  test('does not block rendering when billing check fails open', () => {
    mockedUseBillingPlanAvailability.mockReturnValue({
      planAvailability: createPlanAvailability(true, true),
      isLoading: false,
      isError: true
    })

    render(<TiDBNextGen />)

    expect(screen.getByText('starter-page')).toBeTruthy()
  })
})
