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

const workflowMock: {
  value: { pendingSprintDraft?: Record<string, unknown> } | undefined
} = { value: undefined }

vi.mock('@/stores/user-settings/store', () => ({
  useUserSettingsStore: (
    selector: (s: {
      entities: {
        settings: {
          workflow: { pendingSprintDraft?: Record<string, unknown> } | undefined
        }
      }
    }) => unknown,
  ) => selector({ entities: { settings: { workflow: workflowMock.value } } }),
}))

vi.mock('./new-sprint-metadata-dialog', () => ({
  NewSprintMetadataDialog: () => null,
}))

vi.mock('@/components/shared/demo-tooltip', () => ({
  DemoTooltip: ({ children }: { children: ReactNode }) => children,
}))

import { NewSprintTrigger } from '@/components/backlog/new-sprint-trigger'

beforeEach(() => {
  workflowMock.value = undefined
})

describe('NewSprintTrigger', () => {
  it('renders the button on an active product without a draft', () => {
    render(<NewSprintTrigger productId="p1" isDemo={false} isActiveProduct={true} />)
    expect(screen.getByText('Nieuwe sprint')).toBeInTheDocument()
  })

  it('renders nothing on a non-active product (G6)', () => {
    const { container } = render(
      <NewSprintTrigger productId="p1" isDemo={false} isActiveProduct={false} />,
    )
    expect(container).toBeEmptyDOMElement()
  })

  it('renders nothing when a sprint draft is pending', () => {
    workflowMock.value = { pendingSprintDraft: { p1: { goal: 'X' } } }
    const { container } = render(
      <NewSprintTrigger productId="p1" isDemo={false} isActiveProduct={true} />,
    )
    expect(container).toBeEmptyDOMElement()
  })
})
