import { render, screen } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DiagnosticsPage } from '@/features/diagnostics/DiagnosticsPage'
import type { Diagnostics } from '@/features/diagnostics/types'

const minimalDiagnostics: Diagnostics = {
  version: { version: '1.1.0', commit: 'abc12345', buildDate: '2026-05-28' },
  health: { live: true, ready: true },
  storage: {
    dbPath: '/tmp/argus.db',
    dbSizeBytes: 0,
    totalEvents: 0,
    totalSessions: 0,
    latestEventAt: null,
  },
  agents: [],
  privacy: {
    ignoreFile: { path: '', status: 'missing_ok', activePatternCount: 0 },
    exportWarning: 'Exported data may contain sensitive content.',
  },
  security: {
    remoteBind: { addr: '127.0.0.1:10804', status: 'loopback', allowRemote: false },
    cors: { totalOrigins: 1, localOrigins: 1, extraOrigins: 0 },
  },
  fileSystem: {
    argusDir: '/home/user/.argus',
    binary: {
      name: 'argus',
      path: '/home/user/.argus/bin/argus',
      sizeBytes: null,
      lastModified: null,
      exists: false,
    },
    logs: [
      {
        name: 'argus.log',
        path: '/home/user/.argus/argus.log',
        sizeBytes: null,
        lastModified: null,
        exists: false,
      },
      {
        name: 'build.log',
        path: '/home/user/.argus/build.log',
        sizeBytes: null,
        lastModified: null,
        exists: false,
      },
      {
        name: 'hook-scripts.log',
        path: '/home/user/.argus/hook-scripts.log',
        sizeBytes: 128,
        lastModified: '2026-06-10T00:00:00Z',
        exists: true,
      },
    ],
    hooks: [],
    claudeDir: '/home/user/.claude',
    claudeDirExists: true,
    claudeHooks: [],
    claudeHooksDirExists: true,
    claudeHistory: {
      name: 'history.jsonl',
      path: '/home/user/.claude/history.jsonl',
      sizeBytes: 278000,
      lastModified: '2026-06-09T10:00:00Z',
      exists: true,
      lineCount: 48231,
    },
    codexDir: '/home/user/.codex',
    codexDirExists: false,
    codexHooks: [],
    codexHooksDirExists: false,
    codexDBs: [],
    codexDBsDirExists: false,
  },
  runtime: {
    startedAt: '2026-06-09T08:00:00Z',
    uptimeSeconds: 3600,
    hookRequests: 150,
    ingestionErrors: 0,
  },
  dbHealth: {
    journalMode: 'wal',
    pageCount: 1024,
    pageSizeBytes: 4096,
    walSizeBytes: 65536,
    migrationVersion: 13,
  },
}

beforeEach(() => {
  vi.clearAllMocks()
  Object.defineProperty(navigator, 'clipboard', {
    value: { writeText: vi.fn() },
    writable: true,
  })
  vi.stubGlobal(
    'fetch',
    vi.fn().mockResolvedValue({ ok: true, json: async () => minimalDiagnostics })
  )
})

afterEach(() => {
  vi.clearAllMocks()
})

describe('DiagnosticsPage route', () => {
  it('renders the Diagnostics heading when mounted', () => {
    render(
      <MemoryRouter initialEntries={['/diagnostics']}>
        <DiagnosticsPage />
      </MemoryRouter>
    )
    // h1 heading is always present regardless of loading state
    expect(screen.getByRole('heading', { name: 'Diagnostics' })).toBeInTheDocument()
  })

  it('renders the page heading in all loading states', async () => {
    vi.stubGlobal('fetch', vi.fn().mockReturnValue(new Promise(() => {})))
    render(
      <MemoryRouter>
        <DiagnosticsPage />
      </MemoryRouter>
    )
    expect(screen.getByRole('heading', { name: 'Diagnostics' })).toBeInTheDocument()
  })
})
