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

import React from 'react';
import { FaCog, FaPlay, FaSpinner, FaCloudUploadAlt, FaCloudDownloadAlt, FaArchive } from 'react-icons/fa';
import type { Automation2Workflow, ExecuteGraphResponse, AutoVersion } from '../../../api/workflowApi';
import styles from './Automation2FlowEditor.module.css';

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;
}

const STATUS_BADGE: Record<string, { label: string; color: string }> = {
  draft: { label: 'Entwurf', color: 'var(--warning-color, #ffc107)' },
  published: { label: 'Veröffentlicht', color: 'var(--success-color, #28a745)' },
  archived: { label: '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,
}) => {
  const currentVersion = versions?.find((v) => v.id === currentVersionId);
  const currentStatus = currentVersion?.status || 'draft';
  const badge = STATUS_BADGE[currentStatus] || STATUS_BADGE.draft;

  return (
    <div className={styles.canvasHeader}>
      <div style={{ display: 'flex', alignItems: 'center', gap: '1rem', flexWrap: 'wrap' }}>
        <h4 className={styles.canvasTitle} style={{ margin: 0 }}>
          Workflow-Editor
        </h4>
        {onWorkflowSettings && (
          <button
            type="button"
            className={styles.canvasGearBtn}
            title="Workflow-Konfiguration (Einstieg / Starts)"
            aria-label="Workflow-Konfiguration"
            onClick={onWorkflowSettings}
          >
            <FaCog />
          </button>
        )}
        <button type="button" className={styles.retryButton} onClick={onNew}>
          Neu
        </button>
        <button
          type="button"
          className={styles.retryButton}
          onClick={onSave}
          disabled={saving || !hasNodes}
        >
          {saving ? <FaSpinner className={styles.spinner} /> : 'Speichern'}
        </button>
        <select
          value={currentWorkflowId ?? ''}
          onChange={(e) => {
            const id = e.target.value ? e.target.value : null;
            onWorkflowSelect(id);
          }}
          style={{ padding: '0.4rem', minWidth: 180 }}
        >
          <option value="">— 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' }} />
              Ausführen…
            </>
          ) : (
            <>
              <FaPlay style={{ marginRight: '0.5rem' }} />
              Ausführen
            </>
          )}
        </button>
        {onToggleChat && (
          <button type="button" className={styles.retryButton} onClick={onToggleChat} title="AI Chat öffnen">
            Chat
          </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)' }}>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="">— Aktuelle —</option>
            {versions.map((v) => (
              <option key={v.id} value={v.id}>
                v{v.versionNumber} ({STATUS_BADGE[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="Version veröffentlichen"
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              <FaCloudUploadAlt style={{ marginRight: 4 }} />
              Publish
            </button>
          )}
          {currentVersion && currentStatus === 'published' && onUnpublishVersion && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => onUnpublishVersion(currentVersion.id)}
              disabled={versionLoading}
              title="Veröffentlichung zurücknehmen"
              style={{ fontSize: '0.8rem', padding: '0.25rem 0.6rem' }}
            >
              <FaCloudDownloadAlt style={{ marginRight: 4 }} />
              Unpublish
            </button>
          )}
          {currentVersion && currentStatus !== 'archived' && onArchiveVersion && (
            <button
              type="button"
              className={styles.retryButton}
              onClick={() => onArchiveVersion(currentVersion.id)}
              disabled={versionLoading}
              title="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="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 ? (
            <>✓ Ausführung abgeschlossen.</>
          ) : (executeResult as { paused?: boolean }).paused ? (
            <>
              ⏸ Workflow pausiert. Öffne <strong>Workflows & Tasks</strong> in der Sidebar, um den
              Task zu bearbeiten.
            </>
          ) : (
            <>✗ {executeResult.error ?? 'Unbekannter Fehler'}</>
          )}
        </div>
      )}
    </div>
  );
};
