'use client'

import { GlassCard } from './GlassCard'

interface PipelineResult {
  success: boolean
  handwriting_detected: boolean
  handwriting_removed: boolean
  layout_reconstructed: boolean
  cleaned_image_base64?: string
  fabric_json?: any
  metadata: any
}

interface ResultStepProps {
  pipelineResult: PipelineResult
  previewUrl: string | null
  cleanedUrl: string | null
  onReset: () => void
  onOpenInEditor: () => void
}

export function ResultStep({ pipelineResult, previewUrl, cleanedUrl, onReset, onOpenInEditor }: ResultStepProps) {
  return (
    <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
      <GlassCard className="col-span-1 lg:col-span-2" delay={100}>
        <div className={`flex items-center gap-4 ${pipelineResult.success ? 'text-green-300' : 'text-red-300'}`}>
          <div className={`w-12 h-12 rounded-full flex items-center justify-center ${pipelineResult.success ? 'bg-green-500/20' : 'bg-red-500/20'}`}>
            {pipelineResult.success ? (
              <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /></svg>
            ) : (
              <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /></svg>
            )}
          </div>
          <div>
            <h3 className="text-xl font-semibold">
              {pipelineResult.success ? 'Erfolgreich bereinigt!' : 'Verarbeitung fehlgeschlagen'}
            </h3>
            <p className="text-white/50">
              {pipelineResult.handwriting_removed
                ? `Handschrift wurde entfernt. ${pipelineResult.metadata?.layout?.element_count || 0} Elemente erkannt.`
                : pipelineResult.handwriting_detected
                  ? 'Handschrift erkannt, aber nicht entfernt'
                  : 'Keine Handschrift im Bild gefunden'}
            </p>
          </div>
        </div>
      </GlassCard>

      <GlassCard delay={200}>
        <h3 className="text-lg font-semibold text-white mb-4">Original</h3>
        {previewUrl && <img src={previewUrl} alt="Original" className="w-full rounded-xl" />}
      </GlassCard>

      <GlassCard delay={300}>
        <h3 className="text-lg font-semibold text-white mb-4">Bereinigt</h3>
        {cleanedUrl ? (
          <img src={cleanedUrl} alt="Cleaned" className="w-full rounded-xl" />
        ) : (
          <div className="aspect-video rounded-xl bg-white/5 flex items-center justify-center text-white/40">Kein Bild</div>
        )}
      </GlassCard>

      <div className="col-span-1 lg:col-span-2 flex justify-center gap-4">
        <button onClick={onReset}
          className="px-6 py-3 rounded-xl bg-white/10 text-white hover:bg-white/20 transition-all flex items-center gap-2">
          <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /></svg>
          Neues Bild
        </button>
        {cleanedUrl && (
          <a href={cleanedUrl} download="bereinigt.png"
            className="px-6 py-3 rounded-xl bg-white/10 text-white hover:bg-white/20 transition-all flex items-center gap-2">
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" /></svg>
            Download
          </a>
        )}
        {pipelineResult.layout_reconstructed && pipelineResult.fabric_json && (
          <button onClick={onOpenInEditor}
            className="px-8 py-3 rounded-xl font-semibold bg-gradient-to-r from-green-500 to-emerald-500 text-white hover:shadow-lg hover:shadow-green-500/30 transition-all flex items-center gap-2">
            <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" /></svg>
            Im Editor öffnen
          </button>
        )}
      </div>
    </div>
  )
}
