/**
 * CloudStoragePage.test.tsx — landing page for /cloud/storage (P3).
 */

import { describe, it, expect, afterEach, beforeEach } from 'vitest'
import { render, screen, cleanup } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
  RouterProvider,
  createRouter,
  createRootRoute,
  createRoute,
  createMemoryHistory,
  Outlet,
} from '@tanstack/react-router'

import { CloudPage } from '../CloudPage'
import { CloudStoragePage } from './CloudStoragePage'
import { infrastructureTopologyFixture } from '@/test/fixtures/infrastructure-topology.fixture'
import type { HierarchicalInfrastructure } from '@/lib/infrastructure.types'
import { useWizardStore } from '@/entities/deployment/store'
import { INITIAL_WIZARD_STATE } from '@/entities/deployment/model'

function renderLanding(data: HierarchicalInfrastructure) {
  useWizardStore.setState({ ...INITIAL_WIZARD_STATE })
  globalThis.fetch = (() =>
    Promise.resolve({
      ok: true,
      json: () => Promise.resolve({ events: [], state: undefined, done: false }),
    } as unknown as Response)) as typeof fetch

  const rootRoute = createRootRoute({ component: () => <Outlet /> })
  const cloudRoute = createRoute({
    getParentRoute: () => rootRoute,
    path: '/provision/$deploymentId/cloud',
    component: () => <CloudPage disableStream initialDataOverride={data} deploymentsOverride={[]} />,
  })
  const stRoute = createRoute({
    getParentRoute: () => cloudRoute,
    path: '/storage',
    component: CloudStoragePage,
  })
  const tree = rootRoute.addChildren([cloudRoute.addChildren([stRoute])])
  const router = createRouter({
    routeTree: tree,
    history: createMemoryHistory({
      initialEntries: ['/provision/d-1/cloud/storage'],
    }),
  })
  const qc = new QueryClient({
    defaultOptions: { queries: { retry: false, gcTime: 0 } },
  })
  return render(
    <QueryClientProvider client={qc}>
      <RouterProvider router={router} />
    </QueryClientProvider>,
  )
}

beforeEach(() => {
  if (typeof window !== 'undefined') window.localStorage.clear()
})
afterEach(() => cleanup())

describe('CloudStoragePage', () => {
  it('renders 4 tiles (pvcs / storage-classes / buckets / volumes)', async () => {
    renderLanding(infrastructureTopologyFixture)
    expect(await screen.findByTestId('cloud-storage-page-tile-pvcs')).toBeTruthy()
    expect(screen.getByTestId('cloud-storage-page-tile-storage-classes')).toBeTruthy()
    expect(screen.getByTestId('cloud-storage-page-tile-buckets')).toBeTruthy()
    expect(screen.getByTestId('cloud-storage-page-tile-volumes')).toBeTruthy()
  })

  it('counts derive from the fixture (2 pvcs, 1 bucket, 1 volume)', async () => {
    renderLanding(infrastructureTopologyFixture)
    expect((await screen.findByTestId('cloud-storage-page-tile-pvcs-count')).textContent).toBe('2')
    expect(screen.getByTestId('cloud-storage-page-tile-buckets-count').textContent).toBe('1')
    expect(screen.getByTestId('cloud-storage-page-tile-volumes-count').textContent).toBe('1')
  })

  it('Storage Classes shows — placeholder count', async () => {
    renderLanding(infrastructureTopologyFixture)
    expect((await screen.findByTestId('cloud-storage-page-tile-storage-classes-count')).textContent).toBe('—')
  })
})
