// @vitest-environment jsdom
import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import JobCard from '@/components/jobs/job-card'

const BASE_PROPS = {
  id: 'job-1',
  kind: 'TASK_IMPLEMENTATION' as const,
  status: 'RUNNING' as const,
  productName: 'Scrum4Me',
  productCode: 'S4M',
  pbiCode: 'PBI-1',
  storyCode: 'ST-1',
  createdAt: new Date('2026-01-01T10:00:00Z'),
}

describe('JobCard breadcrumb', () => {
  it('TASK-job toont productCode, pbiCode en storyCode in de breadcrumb', () => {
    render(<JobCard {...BASE_PROPS} />)
    const breadcrumb = screen.getByText('S4M PBI-1 ST-1')
    expect(breadcrumb).toBeInTheDocument()
  })

  it('TASK-job zonder productCode valt terug op productName in de breadcrumb', () => {
    render(<JobCard {...BASE_PROPS} productCode={null} />)
    expect(screen.getByText('Scrum4Me PBI-1 ST-1')).toBeInTheDocument()
  })

  it('TASK-job laat ontbrekende codes weg uit de breadcrumb', () => {
    render(<JobCard {...BASE_PROPS} pbiCode={null} storyCode={null} />)
    expect(screen.getByText('S4M')).toBeInTheDocument()
  })

  it('GRILL-job toont productCode en ideaCode', () => {
    render(
      <JobCard
        {...BASE_PROPS}
        kind="IDEA_GRILL"
        productCode="S4M"
        ideaCode="IDEA-5"
        pbiCode={null}
        storyCode={null}
      />,
    )
    expect(screen.getByText('S4M IDEA-5')).toBeInTheDocument()
  })

  it('SPRINT-job toont productCode en sprintCode', () => {
    render(
      <JobCard
        {...BASE_PROPS}
        kind="SPRINT_IMPLEMENTATION"
        productCode="S4M"
        sprintCode="SP-3"
        pbiCode={null}
        storyCode={null}
      />,
    )
    expect(screen.getByText('S4M SP-3')).toBeInTheDocument()
  })
})

describe('JobCard datumweergave', () => {
  it('toont finishedAt als die beschikbaar is', () => {
    const finishedAt = new Date('2026-03-15T14:30:00Z')
    render(<JobCard {...BASE_PROPS} startedAt={new Date('2026-03-10T09:00:00Z')} finishedAt={finishedAt} />)
    const formatted = finishedAt.toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' })
    expect(screen.getByText(formatted)).toBeInTheDocument()
  })

  it('toont startedAt als finishedAt ontbreekt', () => {
    const startedAt = new Date('2026-03-10T09:00:00Z')
    render(<JobCard {...BASE_PROPS} startedAt={startedAt} finishedAt={null} />)
    const formatted = startedAt.toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' })
    expect(screen.getByText(formatted)).toBeInTheDocument()
  })

  it('toont createdAt als zowel finishedAt als startedAt ontbreken', () => {
    const createdAt = new Date('2026-01-01T10:00:00Z')
    render(<JobCard {...BASE_PROPS} createdAt={createdAt} startedAt={null} finishedAt={null} />)
    const formatted = createdAt.toLocaleString('nl-NL', { dateStyle: 'short', timeStyle: 'short' })
    expect(screen.getByText(formatted)).toBeInTheDocument()
  })
})
