/**
 * CanvasHeader - Workflow controls (Neu, Speichern, laden, Ausführen), version selector, and execute result.
 */

import React, { useState, useRef, useEffect, useCallback, useMemo } from 'react';
import { FaCog, FaPlay, FaSpinner, FaCloudUploadAlt, FaCloudDownloadAlt, FaArchive, FaDatabase, FaBookmark, FaCaretDown } from 'react-icons/fa';
import type { Automation2Workflow, ExecuteGraphResponse, AutoVersion, AutoTemplateScope } from '../../../api/workflowApi';
import styles from './Automation2FlowEditor.module.css';

import { useLanguage } from '../../../providers/language/LanguageContext';

interface CanvasHeaderProps {
  workflows: Automation2Workflow[];
  currentWorkflowId: string | null;
  onWorkflowSelect: (workflowId: string | null) => void;
  onNew: () => void;
  onSave: () => void;
  onExecute: () => void;
  onWorkflowSettings?: () => void;
  onToggleChat?: () => void;
  saving: boolean;
  executing: boolean;
  hasNodes: boolean;
  executeResult: ExecuteGraphResponse | null;
  versions?: AutoVersion[];
  currentVersionId?: string | null;
  onVersionSelect?: (versionId: string | null) => void;
  onPublishVersion?: (versionId: string) => void;
  onUnpublishVersion?: (versionId: string) => void;
  onArchiveVersion?: (versionId: string) => void;
  onCreateDraft?: () => void;
  versionLoading?: boolean;
  onSaveAsTemplate?: (scope: AutoTemplateScope) => void;
  templateSaving?: boolean;
  onNewFromTemplate?: () => void;
  onWorkflowRename?: (workflowId: string, newName: string) => void;
}

function _getStatusBadge(t: (key: string) => string): Record<string, { label: string; color: string }> {
  return {
    draft: { label: t('Entwurf'), color: 'var(--warning-color, #ffc107)' },
    published: { label: t('Veröffentlicht'), color: 'var(--success-color, #28a745)' },
    archived: { label: t('Archiviert'), color: 'var(--text-secondary, #666)' },
  };
}

export const CanvasHeader: React.FC<CanvasHeaderProps> = ({ workflows,
  currentWorkflowId,
  onWorkflowSelect,
  onNew,
  onSave,
  onExecute,
  onWorkflowSettings,
  onToggleChat,
  saving,
  executing,
  hasNodes,
  executeResult,
  versions,
  currentVersionId,
  onVersionSelect,
  onPublishVersion,
  onUnpublishVersion,
  onArchiveVersion,
  onCreateDraft,
  versionLoading,
  onSaveAsTemplate,
  templateSaving,
  onNewFromTemplate,
  onWorkflowRename,
}) => {
  const { t } = useLanguage();
  const statusBadge = _getStatusBadge(t);
  const currentVersion = versions?.find((v) => v.id === currentVersionId);
  const currentStatus = currentVersion?.status || 'draft';
  const badge = statusBadge[currentStatus] || statusBadge.draft;

  const [newMenuOpen, setNewMenuOpen] = useState(false);
  const newMenuRef = useRef<HTMLDivElement>(null);

  const [templateMenuOpen, setTemplateMenuOpen] = useState(false);
  const templateMenuRef = useRef<HTMLDivElement>(null);

  const [editingName, setEditingName] = useState(false);
  const [nameValue, setNameValue] = useState('');
  const nameInputRef = useRef<HTMLInputElement>(null);

  const currentWorkflow = workflows.find((w) => w.id === currentWorkflowId);

  const _startNameEdit = useCallback(() => {
    if (!currentWorkflowId || !onWorkflowRename) return;
    setNameValue(currentWorkflow?.label || '');
    setEditingName(true);
  }, [currentWorkflowId, currentWorkflow?.label, onWorkflowRename]);

  const _commitNameEdit = useCallback(() => {
    setEditingName(false);
    const trimmed = nameValue.trim();
    if (!trimmed || !currentWorkflowId || !onWorkflowRename) return;
    if (trimmed !== currentWorkflow?.label) {
      onWorkflowRename(currentWorkflowId, trimmed);
    }
  }, [nameValue, currentWorkflowId, currentWorkflow?.label, onWorkflowRename]);

  useEffect(() => {
    if (editingName && nameInputRef.current) {
      nameInputRef.current.focus();
      nameInputRef.current.select();
    }
  }, [editingName]);

  useEffect(() => {
    const _handleClickOutside = (e: MouseEvent) => {
      if (newMenuRef.current && !newMenuRef.current.contains(e.target as Node)) setNewMenuOpen(false);
      if (templateMenuRef.current && !templateMenuRef.current.contains(e.target as Node)) setTemplateMenuOpen(false);
    };
    document.addEventListener('mousedown', _handleClickOutside);
    return () => document.removeEventListener('mousedown', _handleClickOutside);
  }, []);

  const scopeLabels = useMemo(
    () =>
      ({
        user: t('Meine Vorlagen'),
        instance: t('Instanz'),
        mandate: t('Mandant'),
      }) as Record<string, string>,
    [t]
  );

  return (
    <div className={styles.canvasHeader}>
      <div style={{ display: 'flex', alignItems: 'center', gap: '0.75rem', flexWrap: 'wrap' }}>
        {/* Workflow name: inline editable */}
        {currentWorkflowId && currentWorkflow ? (
          editingName ? (
            <input
              ref={nameInputRef}
              value={nameValue}
              onChange={(e) => setNameValue(e.target.value)}
              onBlur={_commitNameEdit}
              onKeyDown={(e) => { if (e.key === 'Enter') _commitNameEdit(); if (e.key === 'Escape') setEditingName(false); }}
              style={{ padding: '0.25rem 0.4rem', fontSize: '0.95rem', fontWeight: 600, border: '1px solid var(--primary-color, #007bff)', borderRadius: 4, outline: 'none', minWidth: 140, maxWidth: 300 }}
            />
          ) : (
            <h4
              className={styles.canvasTitle}
              style={{ margin: 0, cursor: onWorkflowRename ? 'pointer' : 'default', fontSize: '0.95rem', fontWeight: 600 }}
              onClick={_startNameEdit}
              title={onWorkflowRename ? t('Klicken zum Umbenennen') : undefined}
            >
              {currentWorkflow.label}
            </h4>
          )
        ) : (
          <h4 className={styles.canvasTitle} style={{ margin: 0, fontStyle: 'italic', opacity: 0.6 }}>
            {t('Neuer Workflow')}
          </h4>
        )}
        {onWorkflowSettings && (
          <button
            type="button"
            className={styles.canvasGearBtn}
            title={t('Workflowkonfiguration Einstieg/Starts')}
            aria-label={t('Workflow-Konfiguration')}
            onClick={onWorkflowSettings}
          >
            <FaCog />
          </button>
        )}

        {/* Split "Neu" button */}
        <div ref={newMenuRef} style={{ position: 'relative', display: 'inline-block' }}>
          <div style={{ display: 'flex' }}>
            <button type="button" className={styles.retryButton} onClick={onNew} style={{ borderTopRightRadius: 0, borderBottomRightRadius: 0 }}>
              {t('Neu')}
            </button>
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => setNewMenuOpen((p) => !p)}
              style={{ borderTopLeftRadius: 0, borderBottomLeftRadius: 0, paddingLeft: 4, paddingRight: 6, borderLeft: '1px solid rgba(0,0,0,0.15)' }}
              title={t('Neu aus Vorlage')}
            >
              <FaCaretDown style={{ fontSize: '0.7rem' }} />
            </button>
          </div>
          {newMenuOpen && (
            <div style={{ position: 'absolute', top: '100%', left: 0, zIndex: 100, background: 'var(--bg-primary, #fff)', border: '1px solid var(--border-color, #e0e0e0)', borderRadius: 6, boxShadow: '0 4px 12px rgba(0,0,0,0.15)', minWidth: 180, marginTop: 4 }}>
              <button
                type="button"
                onClick={() => { onNew(); setNewMenuOpen(false); }}
                style={{ display: 'block', width: '100%', textAlign: 'left', padding: '8px 12px', border: 'none', background: 'transparent', cursor: 'pointer', fontSize: '0.85rem' }}
              >
                {t('Leerer Workflow')}
              </button>
              {onNewFromTemplate && (
                <button
                  type="button"
                  onClick={() => { onNewFromTemplate(); setNewMenuOpen(false); }}
                  style={{ display: 'block', width: '100%', textAlign: 'left', padding: '8px 12px', border: 'none', background: 'transparent', cursor: 'pointer', fontSize: '0.85rem', borderTop: '1px solid var(--border-color, #e0e0e0)' }}
                >
                  {t('Aus Vorlage…')}
                </button>
              )}
            </div>
          )}
        </div>

        <button
          type="button"
          className={styles.retryButton}
          onClick={onSave}
          disabled={saving || !hasNodes}
        >
          {saving ? <FaSpinner className={styles.spinner} /> : t('Speichern')}
        </button>

        {/* Save as template */}
        {currentWorkflowId && onSaveAsTemplate && (
          <div ref={templateMenuRef} style={{ position: 'relative', display: 'inline-block' }}>
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => setTemplateMenuOpen((p) => !p)}
              disabled={templateSaving}
              title={t('Als Vorlage speichern')}
            >
              {templateSaving ? <FaSpinner className={styles.spinner} /> : <><FaBookmark style={{ marginRight: 4 }} />{t('Als Vorlage')}</>}
            </button>
            {templateMenuOpen && (
              <div style={{ position: 'absolute', top: '100%', left: 0, zIndex: 100, background: 'var(--bg-primary, #fff)', border: '1px solid var(--border-color, #e0e0e0)', borderRadius: 6, boxShadow: '0 4px 12px rgba(0,0,0,0.15)', minWidth: 180, marginTop: 4 }}>
                {(['user', 'instance', 'mandate'] as const).map((s) => (
                  <button
                    key={s}
                    type="button"
                    onClick={() => { onSaveAsTemplate(s); setTemplateMenuOpen(false); }}
                    style={{ display: 'block', width: '100%', textAlign: 'left', padding: '8px 12px', border: 'none', background: 'transparent', cursor: 'pointer', fontSize: '0.85rem', borderTop: s !== 'user' ? '1px solid var(--border-color, #e0e0e0)' : undefined }}
                  >
                    {scopeLabels[s]}
                  </button>
                ))}
              </div>
            )}
          </div>
        )}
        <select
          value={currentWorkflowId ?? ''}
          onChange={(e) => {
            const id = e.target.value ? e.target.value : null;
            onWorkflowSelect(id);
          }}
          style={{ padding: '0.4rem', minWidth: 180 }}
        >
          <option value="">{t('Workflow laden')}</option>
          {workflows.map((w) => (
            <option key={w.id} value={w.id}>
              {w.label}
            </option>
          ))}
        </select>
        <button
          type="button"
          className={styles.retryButton}
          onClick={onExecute}
          disabled={executing || !hasNodes}
        >
          {executing ? (
            <>
              <FaSpinner className={styles.spinner} style={{ marginRight: '0.5rem', display: 'inline-block' }} />
              {t('Ausführen…')}
            </>
          ) : (
            <>
              <FaPlay style={{ marginRight: '0.5rem' }} />
              {t('Ausführen')}
            </>
          )}
        </button>
        {onToggleChat && (
          <button type="button" className={styles.retryButton} onClick={onToggleChat} title={t('Workspace-Panel: Chats, Dateien, Quellen')}>
            <FaDatabase style={{ marginRight: '0.4rem' }} />
            {t('Workspace')}
          </button>
        )}
      </div>

      {/* Version Selector */}
      {currentWorkflowId && versions && versions.length > 0 && (
        <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', marginTop: '0.5rem', flexWrap: 'wrap' }}>
          <span style={{ fontSize: '0.8rem', fontWeight: 600, color: 'var(--text-secondary, #666)' }}>{t('Version:')}</span>
          <select
            value={currentVersionId ?? ''}
            onChange={(e) => onVersionSelect?.(e.target.value || null)}
            style={{ padding: '0.3rem', minWidth: 140, fontSize: '0.85rem' }}
            disabled={versionLoading}
          >
            <option value="">{t('Aktuelle')}</option>
            {versions.map((v) => (
              <option key={v.id} value={v.id}>
                v{v.versionNumber} ({statusBadge[v.status]?.label ?? v.status})
              </option>
            ))}
          </select>
          <span
            style={{
              padding: '2px 8px',
              borderRadius: 10,
              fontSize: '0.75rem',
              fontWeight: 600,
              background: badge.color + '22',
              color: badge.color,
            }}
          >
            {badge.label}
          </span>
          {currentVersion && currentStatus === 'draft' && onPublishVersion && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => onPublishVersion(currentVersion.id)}
              disabled={versionLoading}
              title={t('Version veröffentlichen')}
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              <FaCloudUploadAlt style={{ marginRight: 4 }} />
              {t('Veröffentlichen')}
            </button>
          )}
          {currentVersion && currentStatus === 'published' && onUnpublishVersion && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => onUnpublishVersion(currentVersion.id)}
              disabled={versionLoading}
              title={t('Veröffentlichung zurücknehmen')}
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              <FaCloudDownloadAlt style={{ marginRight: 4 }} />
              {t('Veröffentlichung aufheben')}
            </button>
          )}
          {currentVersion && currentStatus !== 'archived' && onArchiveVersion && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => onArchiveVersion(currentVersion.id)}
              disabled={versionLoading}
              title={t('Version archivieren')}
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              <FaArchive style={{ marginRight: 4 }} />
              Archiv
            </button>
          )}
          {onCreateDraft && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={onCreateDraft}
              disabled={versionLoading}
              title={t('Neuen Entwurf erstellen')}
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              + Entwurf
            </button>
          )}
          {versionLoading && <FaSpinner className={styles.spinner} style={{ fontSize: '0.85rem' }} />}
        </div>
      )}

      {executeResult && (
        <div
          style={{
            marginTop: '0.5rem',
            padding: '0.5rem',
            borderRadius: 6,
            fontSize: '0.875rem',
            background: executeResult.success
              ? 'rgba(40,167,69,0.15)'
              : (executeResult as { paused?: boolean }).paused
                ? 'rgba(0,123,255,0.15)'
                : 'rgba(220,53,69,0.15)',
            color: executeResult.success
              ? 'var(--success-color,#28a745)'
              : (executeResult as { paused?: boolean }).paused
                ? 'var(--primary-color,#007bff)'
                : 'var(--danger-color,#dc3545)',
          }}
        >
          {executeResult.success ? (
            <>{t('Ausführung abgeschlossen')}</>
          ) : (executeResult as { paused?: boolean }).paused ? (
            <>
              ⏸ Workflow pausiert. Öffne <strong>{t('Workflows/Tasks')}</strong> in der Sidebar, um den
              Task zu bearbeiten.
            </>
          ) : (
            <>✗ {executeResult.error ?? t('Unbekannter Fehler')}</>
          )}
        </div>
      )}
    </div>
  );
};
