'use client'

// Shared "Als Wertstrom übernehmen" trigger (KAR-972) — used by BOTH entry
// points (qaf-differences detail + project/[id]/qaf list). Opens the shared
// QvsImportPreviewDialog. `sources` is already pre-filtered to ELIGIBLE-only
// candidates by the caller (server-side, via getQafValueStreamCapability) —
// "kein toter Button" (ux-flow.md §2): this component never renders
// anything for a source that isn't eligible, because it never receives one.
//
// tdd-guard:skip — thin trigger/selector shell; the ALT/NEU default-role
// logic it delegates to is unit-tested in
// __tests__/qvs-entry-point-sources.test.ts, and the dialog it opens has its
// own component tests.

import { useState } from 'react'
import { GitBranch } from 'lucide-react'
import { Button } from '@/components/ui/button'
import QvsImportPreviewDialog from './qvs-import-preview-dialog'
import {
  defaultEntryPointRole,
  type QvsEntrySource,
} from './qvs-entry-point-sources'
import { useI18n } from '@/lib/i18n/i18n-context'

export interface QvsEntryPointButtonProps {
  sources: QvsEntrySource[]
  titleParts?: {
    supplier?: string | null
    project?: string | null
    variant?: string | null
  }
  className?: string
}

export default function QvsEntryPointButton({
  sources,
  titleParts,
  className,
}: QvsEntryPointButtonProps) {
  const { t } = useI18n()
  const defaultRole = defaultEntryPointRole(sources)
  const [selectedRole, setSelectedRole] = useState(defaultRole)
  const [open, setOpen] = useState(false)

  if (sources.length === 0 || !selectedRole) return null

  const selected = sources.find((s) => s.role === selectedRole) ?? sources[0]

  return (
    <span className={`inline-flex items-center gap-1.5 ${className ?? ''}`}>
      {sources.length > 1 && (
        <span
          className="inline-flex overflow-hidden rounded-sm border border-input text-[11px]"
          role="group"
          aria-label={t('wertstrom.qvs.chooseAltNeuSide', 'ALT- oder NEU-Seite wählen')}
        >
          {sources.map((s) => (
            <button
              key={s.role}
              type="button"
              onClick={() => setSelectedRole(s.role)}
              title={s.fileLabel}
              aria-pressed={selectedRole === s.role}
              className={`px-2 py-1 font-medium transition-colors ${
                selectedRole === s.role
                  ? 'bg-primary text-white'
                  : 'bg-background text-muted-foreground hover:text-foreground'
              }`}
            >
              {s.role === 'alt' ? t('wertstrom.qvs.roleAlt', 'ALT') : t('wertstrom.qvs.roleNeu', 'NEU')}
            </button>
          ))}
        </span>
      )}
      <Button
        type="button"
        variant="outline"
        size="sm"
        onClick={() => setOpen(true)}
      >
        <GitBranch size={14} />
        {t('wertstrom.preview.adoptAsValueStream', 'Als Wertstrom übernehmen')}
      </Button>
      {open && selected && (
        <QvsImportPreviewDialog
          qafFileId={selected.qafFileId}
          titleParts={titleParts}
          onClose={() => setOpen(false)}
        />
      )}
    </span>
  )
}
