'use client'

import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { DemoTooltip } from '@/components/shared/demo-tooltip'
import { useUserSettingsStore } from '@/stores/user-settings/store'
import { NewSprintMetadataDialog } from './new-sprint-metadata-dialog'

interface NewSprintTriggerProps {
  productId: string
  isDemo: boolean
}

/**
 * PBI-79 / ST-1337: trigger-knop voor de nieuwe sprint-flow.
 * Verbergt zichzelf wanneer er al een pendingSprintDraft loopt — dan
 * staat de SprintDefinitionBanner zelf de afronding te regelen.
 */
export function NewSprintTrigger({ productId, isDemo }: NewSprintTriggerProps) {
  const [open, setOpen] = useState(false)
  const hasDraft = useUserSettingsStore(
    (s) => !!s.entities.settings.workflow?.pendingSprintDraft?.[productId],
  )

  if (hasDraft) return null

  return (
    <>
      <DemoTooltip show={isDemo}>
        <Button
          size="sm"
          onClick={() => setOpen(true)}
          disabled={isDemo}
          data-debug-id="new-sprint-trigger"
        >
          Nieuwe sprint
        </Button>
      </DemoTooltip>
      <NewSprintMetadataDialog
        open={open}
        productId={productId}
        onOpenChange={setOpen}
      />
    </>
  )
}
