import { useEffect, useState } from 'react'
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'
import atomOneDark from 'react-syntax-highlighter/dist/esm/styles/hljs/atom-one-dark'
import { X, Copy, Check, BookmarkPlus } from 'lucide-react'

const LANGUAGE_MAP: Record<string, string> = {
  powershell: 'powershell',
  bash: 'bash',
  python: 'python',
}

const LANGUAGE_LABELS: Record<string, string> = {
  powershell: 'PowerShell',
  bash: 'Bash',
  python: 'Python',
}

interface ScriptPreviewModalProps {
  script: string
  filename: string | null
  language: string
  onClose: () => void
  onSave: () => void
}

export function ScriptPreviewModal({
  script,
  filename,
  language,
  onClose,
  onSave,
}: ScriptPreviewModalProps) {
  const [copied, setCopied] = useState(false)
  const hlLanguage = LANGUAGE_MAP[language] || 'powershell'
  const lineCount = script.split('\n').length

  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'Escape') onClose()
    }
    document.addEventListener('keydown', handleKeyDown)
    return () => document.removeEventListener('keydown', handleKeyDown)
  }, [onClose])

  const handleCopy = async () => {
    try {
      await navigator.clipboard.writeText(script)
      setCopied(true)
      setTimeout(() => setCopied(false), 2000)
    } catch {
      // clipboard not available
    }
  }

  return (
    <div
      className="fixed inset-0 z-50 bg-black/40 flex items-center justify-center"
      onClick={(e) => { if (e.target === e.currentTarget) onClose() }}
    >
      <div className="bg-card rounded-xl border border-border max-w-[900px] w-full mx-4 max-h-[85vh] flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between px-5 py-3.5 border-b border-border">
          <div className="flex items-center gap-3 min-w-0">
            <span className="font-mono text-sm text-accent-text truncate">
              {filename || 'script'}
            </span>
            <span className="shrink-0 font-mono text-[0.625rem] uppercase tracking-wider px-2 py-0.5 rounded-full bg-[var(--color-bg-elevated)] text-muted-foreground">
              {LANGUAGE_LABELS[language] || language}
            </span>
          </div>
          <div className="flex items-center gap-2">
            <button
              onClick={handleCopy}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium transition-colors border border-border text-foreground hover:border-[var(--color-border-hover)] hover:bg-[var(--color-bg-elevated)]"
            >
              {copied ? <Check size={14} className="text-success" /> : <Copy size={14} />}
              {copied ? 'Copied' : 'Copy'}
            </button>
            <button
              onClick={onSave}
              className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium transition-colors bg-accent text-white hover:brightness-110"
            >
              <BookmarkPlus size={14} />
              Save to Library
            </button>
            <button
              onClick={onClose}
              className="p-1.5 rounded-lg text-muted-foreground hover:text-foreground hover:bg-[var(--color-bg-elevated)] transition-colors"
            >
              <X size={18} />
            </button>
          </div>
        </div>

        {/* Code body */}
        <div className="flex-1 overflow-auto min-h-0">
          <SyntaxHighlighter
            language={hlLanguage}
            style={atomOneDark}
            showLineNumbers
            customStyle={{
              background: 'transparent',
              padding: '16px',
              margin: 0,
              fontSize: '0.8125rem',
              lineHeight: '1.6',
            }}
            lineNumberStyle={{
              color: '#5a6170',
              minWidth: '2.5em',
              paddingRight: '1em',
              userSelect: 'none',
            }}
            wrapLongLines
          >
            {script}
          </SyntaxHighlighter>
        </div>

        {/* Footer */}
        <div className="flex items-center justify-between px-5 py-3 border-t border-border">
          <span className="font-mono text-[0.625rem] text-muted-foreground">
            {lineCount} line{lineCount !== 1 ? 's' : ''}
          </span>
          <button
            onClick={onClose}
            className="px-4 py-1.5 rounded-lg text-xs font-medium transition-colors border border-border text-foreground hover:border-[var(--color-border-hover)] hover:bg-[var(--color-bg-elevated)]"
          >
            Close & Return to Chat
          </button>
        </div>
      </div>
    </div>
  )
}
