'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
  isActiveProduct: 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 — en
 * wanneer het product niet het actieve product is (ST-1369 / G6).
 */
export function NewSprintTrigger({
  productId,
  isDemo,
  isActiveProduct,
}: NewSprintTriggerProps) {
  const [open, setOpen] = useState(false)
  const hasDraft = useUserSettingsStore(
    (s) => !!s.entities.settings.workflow?.pendingSprintDraft?.[productId],
  )

  if (hasDraft) return null
  if (!isActiveProduct) 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}
      />
    </>
  )
}
