/**
 * HandApprovalModal - Modal for approving/rejecting Hand executions
 *
 * Provides detailed view of Hand execution request with:
 * - Hand name and description
 * - Trigger parameters
 * - Expected output/impact
 * - Risk level indicator
 * - Approval timeout countdown
 * - Approve/Reject buttons
 */

import { useState, useEffect, useCallback, useMemo } from 'react';
import {
  X,
  AlertTriangle,
  CheckCircle,
  XCircle,
  Clock,
  Loader2,
  Shield,
  Zap,
  Info,
} from 'lucide-react';
import type { HandRun } from '../store/handStore';
import { HAND_DEFINITIONS, type HandId } from '../types/hands';

// === Types ===

export type RiskLevel = 'low' | 'medium' | 'high';

export interface HandApprovalData {
  runId: string;
  handId: HandId;
  handName: string;
  description: string;
  params: Record<string, unknown>;
  riskLevel: RiskLevel;
  expectedImpact?: string;
  requestedAt: string;
  requestedBy?: string;
  timeoutSeconds?: number;
}

interface HandApprovalModalProps {
  handRun: HandRun | null;
  isOpen: boolean;
  onApprove: (runId: string) => Promise<void>;
  onReject: (runId: string, reason: string) => Promise<void>;
  onClose: () => void;
}

// === Risk Level Config ===

const RISK_CONFIG: Record<
  RiskLevel,
  { label: string; color: string; bgColor: string; borderColor: string; icon: typeof AlertTriangle }
> = {
  low: {
    label: 'Low Risk',
    color: 'text-green-600 dark:text-green-400',
    bgColor: 'bg-green-100 dark:bg-green-900/30',
    borderColor: 'border-green-300 dark:border-green-700',
    icon: CheckCircle,
  },
  medium: {
    label: 'Medium Risk',
    color: 'text-yellow-600 dark:text-yellow-400',
    bgColor: 'bg-yellow-100 dark:bg-yellow-900/30',
    borderColor: 'border-yellow-300 dark:border-yellow-700',
    icon: AlertTriangle,
  },
  high: {
    label: 'High Risk',
    color: 'text-red-600 dark:text-red-400',
    bgColor: 'bg-red-100 dark:bg-red-900/30',
    borderColor: 'border-red-300 dark:border-red-700',
    icon: AlertTriangle,
  },
};

// === Helper Functions ===

function calculateRiskLevel(handId: HandId, params: Record<string, unknown>): RiskLevel {
  // Risk assessment based on Hand type and parameters
  switch (handId) {
    case 'browser':
      // Browser automation can be high risk if interacting with sensitive sites
      const url = String(params.url || '').toLowerCase();
      if (url.includes('bank') || url.includes('payment') || url.includes('admin')) {
        return 'high';
      }
      if (params.headless === false) {
        return 'medium'; // Non-headless mode is more visible
      }
      return 'low';

    case 'twitter':
      // Twitter actions can have public impact
      const action = String(params.action || '');
      if (action === 'post' || action === 'engage') {
        return 'high'; // Public posting is high impact
      }
      return 'medium';

    case 'collector':
      // Data collection depends on scope
      if (params.pagination === true) {
        return 'medium'; // Large scale collection
      }
      return 'low';

    case 'lead':
      // Lead generation accesses external data
      return 'medium';

    case 'clip':
      // Video processing is generally safe
      return 'low';

    case 'predictor':
      // Predictions are read-only
      return 'low';

    case 'researcher':
      // Research is generally safe
      const depth = String(params.depth || '');
      return depth === 'deep' ? 'medium' : 'low';

    default:
      return 'medium';
  }
}

function getExpectedImpact(handId: HandId, params: Record<string, unknown>): string {
  switch (handId) {
    case 'browser':
      return `Will perform browser automation on ${params.url || 'specified URL'}`;
    case 'twitter':
      if (params.action === 'post') {
        return 'Will post content to Twitter/X publicly';
      }
      if (params.action === 'engage') {
        return 'Will like/reply to tweets';
      }
      return 'Will perform Twitter/X operations';
    case 'collector':
      return `Will collect data from ${params.targetUrl || 'specified source'}`;
    case 'lead':
      return `Will search for leads from ${params.source || 'specified source'}`;
    case 'clip':
      return `Will process video: ${params.inputPath || 'specified input'}`;
    case 'predictor':
      return `Will run prediction on ${params.dataSource || 'specified data'}`;
    case 'researcher':
      return `Will conduct research on: ${params.topic || 'specified topic'}`;
    default:
      return 'Will execute Hand operation';
  }
}

function formatTimeRemaining(seconds: number): string {
  if (seconds <= 0) return 'Expired';
  if (seconds < 60) return `${seconds}s`;
  const minutes = Math.floor(seconds / 60);
  const secs = seconds % 60;
  return `${minutes}m ${secs}s`;
}

// === Countdown Timer Hook ===

function useCountdown(startedAt: string, timeoutSeconds: number = 300) {
  const [timeRemaining, setTimeRemaining] = useState(0);
  const [isExpired, setIsExpired] = useState(false);

  useEffect(() => {
    const started = new Date(startedAt).getTime();
    const expiresAt = started + timeoutSeconds * 1000;

    const updateTimer = () => {
      const now = Date.now();
      const remaining = Math.max(0, Math.floor((expiresAt - now) / 1000));
      setTimeRemaining(remaining);
      setIsExpired(remaining <= 0);
    };

    updateTimer();
    const interval = setInterval(updateTimer, 1000);

    return () => clearInterval(interval);
  }, [startedAt, timeoutSeconds]);

  return { timeRemaining, isExpired };
}

// === Sub-components ===

function RiskBadge({ level }: { level: RiskLevel }) {
  const config = RISK_CONFIG[level];
  const Icon = config.icon;

  return (
    <div
      className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-sm font-medium border ${config.bgColor} ${config.color} ${config.borderColor}`}
    >
      <Icon className="w-3.5 h-3.5" />
      {config.label}
    </div>
  );
}

function TimeoutProgress({ timeRemaining, totalSeconds }: { timeRemaining: number; totalSeconds: number }) {
  const percentage = Math.max(0, Math.min(100, (timeRemaining / totalSeconds) * 100));
  const isUrgent = timeRemaining < 60;

  return (
    <div className="space-y-1.5">
      <div className="flex items-center justify-between text-xs">
        <span className="text-gray-500 dark:text-gray-400 flex items-center gap-1">
          <Clock className="w-3 h-3" />
          Time Remaining
        </span>
        <span
          className={`font-medium ${isUrgent ? 'text-red-600 dark:text-red-400' : 'text-gray-700 dark:text-gray-300'}`}
        >
          {formatTimeRemaining(timeRemaining)}
        </span>
      </div>
      <div className="h-2 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden">
        <div
          className={`h-full transition-all duration-1000 ease-linear rounded-full ${
            isUrgent ? 'bg-red-500' : 'bg-blue-500'
          }`}
          style={{ width: `${percentage}%` }}
        />
      </div>
    </div>
  );
}

function ParamsDisplay({ params }: { params: Record<string, unknown> }) {
  if (!params || Object.keys(params).length === 0) {
    return (
      <p className="text-sm text-gray-500 dark:text-gray-400 italic">No parameters provided</p>
    );
  }

  return (
    <div className="bg-gray-50 dark:bg-gray-900 rounded-lg p-3 font-mono text-xs overflow-x-auto">
      <pre className="text-gray-700 dark:text-gray-300">
        {JSON.stringify(params, null, 2)}
      </pre>
    </div>
  );
}

// === Main Component ===

export function HandApprovalModal({
  handRun,
  isOpen,
  onApprove,
  onReject,
  onClose,
}: HandApprovalModalProps) {
  const [isProcessing, setIsProcessing] = useState(false);
  const [showRejectInput, setShowRejectInput] = useState(false);
  const [rejectReason, setRejectReason] = useState('');
  const [error, setError] = useState<string | null>(null);

  // Parse HandRun to get approval data
  const approvalData = useMemo((): HandApprovalData | null => {
    if (!handRun) return null;

    // Extract hand ID from run data (could be stored in result or need to be passed separately)
    const result = handRun.result as Record<string, unknown> | undefined;
    const handId = (result?.handId as HandId) || 'researcher'; // Default fallback
    const params = (result?.params as Record<string, unknown>) || {};
    const handDef = HAND_DEFINITIONS.find((h) => h.id === handId);

    return {
      runId: handRun.runId,
      handId,
      handName: handDef?.name || handId,
      description: handDef?.description || 'Hand execution request',
      params,
      riskLevel: calculateRiskLevel(handId, params),
      expectedImpact: getExpectedImpact(handId, params),
      requestedAt: handRun.startedAt,
      timeoutSeconds: 300, // 5 minutes default timeout
    };
  }, [handRun]);

  // Timer countdown
  const { timeRemaining, isExpired } = useCountdown(
    approvalData?.requestedAt || new Date().toISOString(),
    approvalData?.timeoutSeconds || 300
  );

  // Handle keyboard escape
  useEffect(() => {
    const handleEscape = (e: KeyboardEvent) => {
      if (e.key === 'Escape' && isOpen) {
        onClose();
      }
    };

    window.addEventListener('keydown', handleEscape);
    return () => window.removeEventListener('keydown', handleEscape);
  }, [isOpen, onClose]);

  // Reset state when modal opens/closes
  useEffect(() => {
    if (isOpen) {
      setShowRejectInput(false);
      setRejectReason('');
      setError(null);
      setIsProcessing(false);
    }
  }, [isOpen]);

  const handleApprove = useCallback(async () => {
    if (!approvalData || isProcessing || isExpired) return;

    setIsProcessing(true);
    setError(null);

    try {
      await onApprove(approvalData.runId);
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to approve');
    } finally {
      setIsProcessing(false);
    }
  }, [approvalData, isProcessing, isExpired, onApprove, onClose]);

  const handleReject = useCallback(async () => {
    if (!approvalData || isProcessing) return;

    if (!showRejectInput) {
      setShowRejectInput(true);
      return;
    }

    if (!rejectReason.trim()) {
      setError('Please provide a reason for rejection');
      return;
    }

    setIsProcessing(true);
    setError(null);

    try {
      await onReject(approvalData.runId, rejectReason.trim());
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to reject');
    } finally {
      setIsProcessing(false);
    }
  }, [approvalData, isProcessing, showRejectInput, rejectReason, onReject, onClose]);

  const handleCancelReject = useCallback(() => {
    setShowRejectInput(false);
    setRejectReason('');
    setError(null);
  }, []);

  if (!isOpen || !approvalData) return null;

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center">
      {/* Backdrop */}
      <div
        className="absolute inset-0 bg-black/50 backdrop-blur-sm"
        onClick={onClose}
      />

      {/* Modal */}
      <div className="relative bg-white dark:bg-gray-800 rounded-xl shadow-2xl w-full max-w-lg mx-4 max-h-[90vh] overflow-hidden flex flex-col">
        {/* Header */}
        <div className="flex items-start justify-between p-4 border-b border-gray-200 dark:border-gray-700 flex-shrink-0">
          <div className="flex items-center gap-3">
            <div className="w-10 h-10 bg-amber-100 dark:bg-amber-900/30 rounded-lg flex items-center justify-center">
              <Shield className="w-5 h-5 text-amber-600 dark:text-amber-400" />
            </div>
            <div>
              <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                Hand Approval Request
              </h2>
              <p className="text-xs text-gray-500 dark:text-gray-400">
                Review and approve Hand execution
              </p>
            </div>
          </div>
          <button
            onClick={onClose}
            className="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        {/* Content */}
        <div className="flex-1 overflow-y-auto p-4 space-y-4">
          {/* Expired Warning */}
          {isExpired && (
            <div className="flex items-center gap-2 p-3 bg-gray-100 dark:bg-gray-900 rounded-lg text-gray-600 dark:text-gray-400">
              <Clock className="w-5 h-5 flex-shrink-0" />
              <span className="text-sm">This approval request has expired</span>
            </div>
          )}

          {/* Hand Info */}
          <div className="space-y-3">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Zap className="w-4 h-4 text-amber-500" />
                <h3 className="font-medium text-gray-900 dark:text-white">
                  {approvalData.handName}
                </h3>
              </div>
              <RiskBadge level={approvalData.riskLevel} />
            </div>
            <p className="text-sm text-gray-600 dark:text-gray-400">
              {approvalData.description}
            </p>
          </div>

          {/* Timeout Progress */}
          {!isExpired && (
            <TimeoutProgress
              timeRemaining={timeRemaining}
              totalSeconds={approvalData.timeoutSeconds || 300}
            />
          )}

          {/* Parameters */}
          <div className="space-y-2">
            <label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
              Execution Parameters
            </label>
            <ParamsDisplay params={approvalData.params} />
          </div>

          {/* Expected Impact */}
          {approvalData.expectedImpact && (
            <div className="space-y-2">
              <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 flex items-center gap-1">
                <Info className="w-3.5 h-3.5" />
                Expected Impact
              </label>
              <p className="text-sm text-gray-600 dark:text-gray-400 bg-blue-50 dark:bg-blue-900/20 p-3 rounded-lg">
                {approvalData.expectedImpact}
              </p>
            </div>
          )}

          {/* Request Info */}
          <div className="text-xs text-gray-500 dark:text-gray-400 space-y-1 pt-2 border-t border-gray-200 dark:border-gray-700">
            <p>Run ID: {approvalData.runId}</p>
            <p>
              Requested: {new Date(approvalData.requestedAt).toLocaleString()}
            </p>
          </div>

          {/* Reject Input */}
          {showRejectInput && (
            <div className="space-y-2">
              <label className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                Rejection Reason <span className="text-red-500">*</span>
              </label>
              <textarea
                value={rejectReason}
                onChange={(e) => setRejectReason(e.target.value)}
                placeholder="Please provide a reason for rejecting this request..."
                className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-md bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-red-500"
                rows={3}
                autoFocus
                disabled={isProcessing}
              />
            </div>
          )}

          {/* Error Message */}
          {error && (
            <div className="flex items-center gap-2 p-3 bg-red-50 dark:bg-red-900/20 rounded-lg text-red-700 dark:text-red-400">
              <AlertTriangle className="w-4 h-4 flex-shrink-0" />
              <span className="text-sm">{error}</span>
            </div>
          )}
        </div>

        {/* Footer */}
        <div className="flex items-center justify-end gap-2 p-4 border-t border-gray-200 dark:border-gray-700 flex-shrink-0">
          {showRejectInput ? (
            <>
              <button
                type="button"
                onClick={handleCancelReject}
                disabled={isProcessing}
                className="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors disabled:opacity-50"
              >
                Cancel
              </button>
              <button
                type="button"
                onClick={handleReject}
                disabled={isProcessing || !rejectReason.trim()}
                className="px-4 py-2 text-sm text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50 flex items-center gap-2"
              >
                {isProcessing ? (
                  <>
                    <Loader2 className="w-4 h-4 animate-spin" />
                    Rejecting...
                  </>
                ) : (
                  <>
                    <XCircle className="w-4 h-4" />
                    Confirm Rejection
                  </>
                )}
              </button>
            </>
          ) : (
            <>
              <button
                type="button"
                onClick={onClose}
                disabled={isProcessing}
                className="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors disabled:opacity-50"
              >
                Close
              </button>
              <button
                type="button"
                onClick={handleReject}
                disabled={isProcessing || isExpired}
                className="px-4 py-2 text-sm border border-red-200 dark:border-red-800 text-red-600 dark:text-red-400 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors disabled:opacity-50 flex items-center gap-2"
              >
                <XCircle className="w-4 h-4" />
                Reject
              </button>
              <button
                type="button"
                onClick={handleApprove}
                disabled={isProcessing || isExpired}
                className="px-4 py-2 text-sm text-white bg-green-600 rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50 flex items-center gap-2"
              >
                {isProcessing ? (
                  <>
                    <Loader2 className="w-4 h-4 animate-spin" />
                    Approving...
                  </>
                ) : (
                  <>
                    <CheckCircle className="w-4 h-4" />
                    Approve
                  </>
                )}
              </button>
            </>
          )}
        </div>
      </div>
    </div>
  );
}

export default HandApprovalModal;
