/**
 * TrusteeAbschlussView
 *
 * Tab-based closing/review page. Currently one tab (year-end check),
 * extensible for future use cases. Follows the same pattern as
 * TrusteeAnalyseView: loads the bootstrapped workflow, executes it,
 * and shows pipeline status inline.
 */

import React, { useState, useEffect, useCallback, useRef } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useCurrentInstance } from '../../../hooks/useCurrentInstance';
import { useToast } from '../../../contexts/ToastContext';
import api from '../../../api';
import styles from './TrusteeViews.module.css';
import { useLanguage } from '../../../providers/language/LanguageContext';

// ---------------------------------------------------------------------------
// Tab definitions
// ---------------------------------------------------------------------------

interface TabDef {
  id: string;
  templateTag: string;
  icon: string;
  color: string;
}

const _TABS: TabDef[] = [
  { id: 'year-end', templateTag: 'template:trustee-year-end-check', icon: '\u2705', color: '#795548' },
];

const _TAB_LABEL_KEYS: Record<string, string> = {
  'year-end': 'Jahresabschluss prüfen',
};

const _TAB_DESCRIPTION_KEYS: Record<string, string> = {
  'year-end':
    'Automatische Prüfungen für den Jahresabschluss: Saldovalidierung, Vorjahresvergleich, gesetzliche Checks.',
};

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

interface WorkflowSummary {
  id: string;
  label: string;
  tags: string[];
}

type RunState = 'idle' | 'starting' | 'running' | 'completed' | 'error';

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

export const TrusteeAbschlussView: React.FC = () => {
  const { t } = useLanguage();
  const { instanceId } = useCurrentInstance();
  const { showSuccess, showError } = useToast();
  const [searchParams, setSearchParams] = useSearchParams();

  const activeTab = searchParams.get('tab') || _TABS[0].id;
  const _setActiveTab = useCallback((tab: string) => {
    setSearchParams({ tab }, { replace: true });
  }, [setSearchParams]);

  const [workflows, setWorkflows] = useState<WorkflowSummary[]>([]);
  const [workflowsLoading, setWorkflowsLoading] = useState(true);

  const [runState, setRunState] = useState<RunState>('idle');
  const [runId, setRunId] = useState<string | null>(null);
  const [runSummary, setRunSummary] = useState('');
  const [runError, setRunError] = useState<string | null>(null);
  const pollTimerRef = useRef<number | null>(null);
  const isPollingRef = useRef(false);

  useEffect(() => {
    if (!instanceId) return;
    const _load = async () => {
      setWorkflowsLoading(true);
      try {
        const res = await api.get(`/api/workflows/${instanceId}/workflows`);
        const items: WorkflowSummary[] = (res.data?.workflows || res.data?.items || []).map((w: any) => ({
          id: w.id,
          label: w.label,
          tags: w.tags || [],
        }));
        setWorkflows(items);
      } catch {
        setWorkflows([]);
      } finally {
        setWorkflowsLoading(false);
      }
    };
    _load();
  }, [instanceId]);

  const _findWorkflow = useCallback((tab: string): WorkflowSummary | undefined => {
    const tabDef = _TABS.find((t) => t.id === tab);
    if (!tabDef) return undefined;
    return workflows.find((w) => w.tags.includes(tabDef.templateTag));
  }, [workflows]);

  const _stopPolling = useCallback(() => {
    if (pollTimerRef.current !== null) {
      window.clearInterval(pollTimerRef.current);
      pollTimerRef.current = null;
    }
    isPollingRef.current = false;
  }, []);

  const _pollRun = useCallback(async (rid: string) => {
    if (!instanceId || !rid || isPollingRef.current) return;
    isPollingRef.current = true;
    try {
      const res = await api.get(`/api/workflows/${instanceId}/runs/${rid}/steps`);
      const steps: any[] = Array.isArray(res?.data?.steps) ? res.data.steps : [];
      const completed = steps.filter((s) => s.status === 'completed');
      const failed = steps.filter((s) => s.status === 'failed');
      const running = steps.filter((s) => s.status === 'running');

      setRunSummary(`${completed.length}/${steps.length} ${t('trusteeAbschluss.stepsCompleted', 'Schritte abgeschlossen')}`);

      if (failed.length > 0) {
        const errMsg = failed[failed.length - 1].error || 'Step failed';
        setRunState('error');
        setRunError(errMsg);
        _stopPolling();
        showError('Pipeline error', errMsg);
        return;
      }
      if (running.length === 0 && completed.length === steps.length && steps.length > 0) {
        setRunState('completed');
        _stopPolling();
        showSuccess(t('trusteeAbschluss.completed', 'Abgeschlossen'), t('trusteeAbschluss.workflowDone', 'Prüfungs-Workflow erfolgreich beendet.'));
        return;
      }
      setRunState('running');
    } catch (err: any) {
      if (err?.response?.status === 404) { setRunState('running'); return; }
      setRunState('error');
      setRunError(err.message || 'Polling failed');
      _stopPolling();
    } finally {
      isPollingRef.current = false;
    }
  }, [instanceId, showError, showSuccess, _stopPolling, t]);

  useEffect(() => {
    if (!instanceId || !runId || (runState !== 'running' && runState !== 'starting')) return;
    void _pollRun(runId);
    pollTimerRef.current = window.setInterval(() => { void _pollRun(runId); }, 3000);
    return () => { _stopPolling(); };
  }, [instanceId, runId, runState, _pollRun, _stopPolling]);

  useEffect(() => () => { _stopPolling(); }, [_stopPolling]);

  useEffect(() => {
    _stopPolling();
    setRunState('idle');
    setRunId(null);
    setRunSummary('');
    setRunError(null);
  }, [activeTab, _stopPolling]);

  const _handleExecute = useCallback(async () => {
    const wf = _findWorkflow(activeTab);
    if (!wf || !instanceId) {
      showError('Error', t('trusteeAbschluss.noWorkflow', 'Kein Workflow für diesen Tab gefunden.'));
      return;
    }
    setRunState('starting');
    setRunError(null);
    setRunSummary(t('trusteeAbschluss.starting', 'Workflow wird gestartet...'));
    try {
      const res = await api.post(`/api/workflows/${instanceId}/execute`, { workflowId: wf.id });
      const rid = res?.data?.runId;
      if (rid) {
        setRunId(rid);
        setRunState('running');
        setRunSummary(`Run ${rid.slice(0, 8)} ${t('trusteeAbschluss.started', 'gestartet')}`);
      } else if (res?.data?.success) {
        setRunState('completed');
        setRunSummary(t('trusteeAbschluss.completedSync', 'Workflow synchron abgeschlossen.'));
        showSuccess(t('trusteeAbschluss.completed', 'Abgeschlossen'), t('trusteeAbschluss.workflowDone', 'Prüfungs-Workflow erfolgreich beendet.'));
      } else {
        throw new Error(res?.data?.error || 'Unexpected response');
      }
    } catch (err: any) {
      const msg = err?.response?.data?.detail || err.message || 'Failed to start workflow';
      setRunState('error');
      setRunError(typeof msg === 'string' ? msg : JSON.stringify(msg));
      showError('Error', typeof msg === 'string' ? msg : JSON.stringify(msg));
    }
  }, [activeTab, instanceId, _findWorkflow, showError, showSuccess, t]);

  const currentTab = _TABS.find((t) => t.id === activeTab) || _TABS[0];
  const currentWorkflow = _findWorkflow(activeTab);

  return (
    <div className={styles.listView}>
      <div className={styles.expenseImportSection}>
        <h3 className={styles.sectionTitle}>{t('trusteeAbschluss.title', 'Abschluss & Prüfung')}</h3>

        {/* Tab bar */}
        {_TABS.length > 1 && (
          <div style={{ display: 'flex', gap: '0.25rem', marginBottom: '1.5rem', borderBottom: '2px solid var(--border-color, #e0e0e0)', paddingBottom: 0 }}>
            {_TABS.map((tab) => (
              <button
                key={tab.id}
                onClick={() => _setActiveTab(tab.id)}
                style={{
                  padding: '0.625rem 1rem',
                  border: 'none',
                  borderBottom: activeTab === tab.id ? `3px solid ${tab.color}` : '3px solid transparent',
                  background: 'transparent',
                  color: activeTab === tab.id ? 'var(--text-primary, #1a1a1a)' : 'var(--text-secondary, #666)',
                  fontWeight: activeTab === tab.id ? 600 : 400,
                  fontSize: '0.875rem',
                  cursor: 'pointer',
                  transition: 'all 0.2s',
                  marginBottom: '-2px',
                }}
              >
                <span style={{ marginRight: '0.375rem' }}>{tab.icon}</span>
                {t(_TAB_LABEL_KEYS[tab.id] || tab.id)}
              </button>
            ))}
          </div>
        )}

        {/* Tab content */}
        <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
          <p className={styles.sectionDescription}>
            {_TAB_DESCRIPTION_KEYS[activeTab] ? t(_TAB_DESCRIPTION_KEYS[activeTab]) : ''}
          </p>

          {workflowsLoading ? (
            <p className={styles.loadingText}>{t('trusteeAbschluss.loadingWorkflows', 'Workflows werden geladen...')}</p>
          ) : !currentWorkflow ? (
            <div className={styles.infoBox}>
              <p>{t('trusteeAbschluss.noWorkflowInfo', 'Für diesen Tab wurde kein Workflow in der Instanz gefunden. Der Workflow wird beim Erstellen der Instanz automatisch angelegt.')}</p>
            </div>
          ) : (
            <>
              <div style={{ display: 'flex', alignItems: 'center', gap: '0.75rem' }}>
                <span style={{ fontSize: '2rem' }}>{currentTab.icon}</span>
                <div>
                  <div style={{ fontWeight: 600, color: 'var(--text-primary, #1a1a1a)' }}>{currentWorkflow.label}</div>
                  <div style={{ fontSize: '0.8125rem', color: 'var(--text-secondary, #666)' }}>
                    Workflow ID: {currentWorkflow.id.slice(0, 8)}...
                  </div>
                </div>
              </div>

              <button
                className={styles.primaryButton}
                onClick={_handleExecute}
                disabled={runState === 'starting' || runState === 'running'}
                style={{ alignSelf: 'flex-start' }}
              >
                {runState === 'starting' || runState === 'running'
                  ? t('trusteeAbschluss.running', 'Läuft...')
                  : t('trusteeAbschluss.execute', 'Prüfung starten')}
              </button>
            </>
          )}

          {runState !== 'idle' && (
            <div className={runState === 'error' ? styles.errorMessage : styles.successMessage}>
              <strong>{t('trusteeAbschluss.status', 'Status')}:</strong>{' '}
              {runState === 'starting' && t('trusteeAbschluss.startingLabel', 'Wird gestartet...')}
              {runState === 'running' && t('trusteeAbschluss.runningLabel', 'Läuft')}
              {runState === 'completed' && t('trusteeAbschluss.completedLabel', 'Abgeschlossen')}
              {runState === 'error' && t('trusteeAbschluss.errorLabel', 'Fehler')}
              {runSummary && <div style={{ marginTop: '0.25rem' }}>{runSummary}</div>}
              {runError && <div style={{ marginTop: '0.25rem' }}>{runError}</div>}
            </div>
          )}
        </div>
      </div>
    </div>
  );
};

export default TrusteeAbschlussView;
