// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'
import type { JobWithRelations } from '@/actions/jobs-page'

vi.mock('@/actions/claude-jobs', () => ({
  restartClaudeJobAction: vi.fn(),
}))

vi.mock('sonner', () => ({ toast: { error: vi.fn() } }))

import { restartClaudeJobAction } from '@/actions/claude-jobs'
import JobDetailPane from '@/components/jobs/job-detail-pane'

const mockAction = restartClaudeJobAction as ReturnType<typeof vi.fn>

function makeJob(status: JobWithRelations['status']): JobWithRelations {
  return {
    id: 'job-1',
    kind: 'TASK_IMPLEMENTATION',
    status,
    taskCode: 'T-1',
    taskTitle: 'Test taak',
    ideaCode: null,
    ideaTitle: null,
    sprintGoal: null,
    sprintCode: null,
    productName: 'Scrum4Me',
    productCode: null,
    storyCode: null,
    pbiCode: null,
    modelId: null,
    inputTokens: null,
    outputTokens: null,
    cacheReadTokens: null,
    cacheWriteTokens: null,
    costUsd: null,
    branch: null,
    prUrl: null,
    error: null,
    summary: null,
    description: null,
    verifyResult: null,
    startedAt: null,
    finishedAt: null,
    createdAt: new Date('2026-01-01'),
    sprintRunId: null,
  }
}

beforeEach(() => {
  vi.clearAllMocks()
  mockAction.mockResolvedValue({ success: true })
})

describe('JobDetailPane restart button', () => {
  it('toont de knop voor FAILED-jobs', () => {
    render(<JobDetailPane job={makeJob('FAILED')} isDemo={false} />)
    expect(screen.getByRole('button', { name: /opnieuw starten/i })).toBeInTheDocument()
  })

  it('toont de knop niet voor DONE-jobs', () => {
    render(<JobDetailPane job={makeJob('DONE')} isDemo={false} />)
    expect(screen.queryByRole('button', { name: /opnieuw starten/i })).not.toBeInTheDocument()
  })

  it('roept restartClaudeJobAction aan met het juiste id bij klik', () => {
    render(<JobDetailPane job={makeJob('FAILED')} isDemo={false} />)
    fireEvent.click(screen.getByRole('button', { name: /opnieuw starten/i }))
    expect(mockAction).toHaveBeenCalledWith('job-1')
  })

  it('knop is disabled in demo-modus', () => {
    render(<JobDetailPane job={makeJob('FAILED')} isDemo={true} />)
    expect(screen.getByRole('button', { name: /opnieuw starten/i })).toBeDisabled()
  })
})
