import { Box, Button, CircularProgress, Divider, Typography } from '@mui/material'
import { ArrowRight, Zap } from 'lucide-react'
import type { MarketSignal } from '../../domain/marketSignal'
import { SignalProcessingStatus } from '../../domain/marketSignal'
import { useConvertToFutureSignal } from '../../hooks/useMarketSignals'

const ELIGIBLE_STATUSES: SignalProcessingStatus[] = [
  SignalProcessingStatus.ENRICHED,
  SignalProcessingStatus.APPROVED_AS_SIGNAL,
]

interface SignalConversionPanelProps {
  signal: MarketSignal
}

export function SignalConversionPanel({ signal }: SignalConversionPanelProps) {
  const { mutate: convert, isPending, isSuccess } = useConvertToFutureSignal()

  if (!ELIGIBLE_STATUSES.includes(signal.processingStatus)) return null

  if (signal.possibleFutureSignalId || isSuccess) {
    return (
      <Box
        sx={{
          p: 2,
          bgcolor: 'rgba(139,92,246,0.08)',
          border: '1px solid rgba(139,92,246,0.2)',
          borderRadius: 1,
          display: 'flex',
          alignItems: 'center',
          gap: 1,
        }}
      >
        <Zap size={16} color="#7c3aed" />
        <Typography variant="body2" sx={{ color: '#7c3aed', fontWeight: 500 }}>
          Bereits in Future Availability überführt
          {signal.possibleFutureSignalId && ` (${signal.possibleFutureSignalId})`}
        </Typography>
      </Box>
    )
  }

  return (
    <Box
      sx={{
        p: 2,
        bgcolor: '#f8fafc',
        border: '1px solid #e2e8f0',
        borderRadius: 1,
      }}
    >
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 1 }}>
        <ArrowRight size={16} color="#152642" />
        <Typography variant="subtitle2" sx={{ fontWeight: 600 }}>
          In Future Availability überführen
        </Typography>
      </Box>

      <Divider sx={{ my: 1 }} />

      <Typography variant="body2" sx={{ color: 'text.secondary', mb: 0.75 }}>
        <strong>Vorgeschlagener Titel:</strong> {signal.title}
      </Typography>
      <Typography variant="body2" sx={{ color: 'text.secondary', mb: 1.5 }}>
        <strong>Begründung:</strong> Signal hat ausreichend Evidenz und Source Reliability, um als
        Future Availability Kandidat geführt zu werden. Manueller Review empfohlen.
      </Typography>

      <Button
        variant="contained"
        size="small"
        disabled={isPending}
        onClick={() => convert(signal.id)}
        startIcon={isPending ? <CircularProgress size={14} sx={{ color: '#fff' }} /> : <Zap size={14} />}
        sx={{ bgcolor: '#7c3aed', textTransform: 'none', '&:hover': { bgcolor: '#6d28d9' } }}
      >
        {isPending ? 'Wird überführt...' : 'Jetzt konvertieren'}
      </Button>
    </Box>
  )
}
