/**
 * Processing mode selection (Fast/Precise) support
 */

import React, { useState, useEffect } from 'react';
import { createPortal } from 'react-dom';
import { ModelConfig, RawFile, IndexingConfig } from '../types';
import { useLanguage } from '../contexts/LanguageContext';
import { useToast } from '../contexts/ToastContext';
import { Layers, FileText, Database, X, ArrowRight, Files, Info, Zap, Target, AlertTriangle, Clock, DollarSign } from 'lucide-react';
import { formatBytes } from '../utils/fileUtils';
import { chunkConfigService } from '../services/chunkConfigService';
import { uploadService } from '../services/uploadService';

interface IndexingModalWithModeProps {
  isOpen: boolean;
  onClose: () => void;
  files?: RawFile[];
  embeddingModels: ModelConfig[];
  defaultEmbeddingId: string;
  onConfirm: (config: IndexingConfig) => void;
  isReconfiguring?: boolean;
}

const IndexingModalWithMode: React.FC<IndexingModalWithModeProps> = ({
  isOpen,
  onClose,
  files = [],
  embeddingModels,
  defaultEmbeddingId,
  onConfirm,
  isReconfiguring = false
}) => {
  const { t } = useLanguage();
  const { showWarning, showInfo } = useToast();

  // Configuration state
  const [chunkSize, setChunkSize] = useState(200);
  const [chunkOverlap, setChunkOverlap] = useState(40);
  const [selectedEmbedding, setSelectedEmbedding] = useState('');
  const [mode, setMode] = useState<'fast' | 'precise'>('fast');
  const [userSelectedMode, setUserSelectedMode] = useState(false); // Track if user manually selected mode

  // Mode recommendation info
  const [modeRecommendation, setModeRecommendation] = useState<any>(null);
  const [isLoadingRecommendation, setIsLoadingRecommendation] = useState(false);

  // Limit info state
  const [limits, setLimits] = useState<{
    maxChunkSize: number;
    maxOverlapSize: number;
    defaultChunkSize: number;
    defaultOverlapSize: number;
    modelInfo: {
      name: string;
      maxInputTokens: number;
      maxBatchSize: number;
      expectedDimensions: number;
    };
  } | null>(null);

  const [isLoadingLimits, setIsLoadingLimits] = useState(false);

  // Get auth token
  const getAuthToken = () => {
    return localStorage.getItem('authToken') || '';
  };

  // Load mode recommendation when files change
  useEffect(() => {
    if (!isOpen || !files || files.length === 0) return;

    const loadRecommendation = async () => {
      setIsLoadingRecommendation(true);
      try {
        // Use first file for recommendation (assume similar types)
        const file = files[0];
        const rec = await uploadService.recommendMode(file.file);
        setModeRecommendation(rec);

        // Auto-select recommended mode if user hasn't manually selected one
        if (!isReconfiguring && !userSelectedMode) {
          setMode(rec.recommendedMode);
          showInfo(t('recommendationMsg', rec.recommendedMode === 'precise' ? t('preciseMode') : t('fastMode'), t(rec.reason, ...(rec.reasonArgs || []))));
        }
      } catch (error) {
        console.error('モード推奨の取得に失敗しました:', error);
      } finally {
        setIsLoadingRecommendation(false);
      }
    };

    loadRecommendation();
  }, [isOpen, files, isReconfiguring]);

  // Load config limits when selected model changes
  useEffect(() => {
    if (!isOpen || !selectedEmbedding) {
      setLimits(null);
      return;
    }

    const loadLimits = async () => {
      setIsLoadingLimits(true);
      try {
        const token = getAuthToken();
        if (!token) return;

        const limitData = await chunkConfigService.getLimits(selectedEmbedding, token);
        setLimits(limitData);

        // Auto-adjust if current values exceed new limits
        if (chunkSize > limitData.maxChunkSize) {
          setChunkSize(limitData.maxChunkSize);
          showWarning(t('autoAdjustChunk', limitData.maxChunkSize));
        }
        if (chunkOverlap > limitData.maxOverlapSize) {
          setChunkOverlap(limitData.maxOverlapSize);
          showWarning(t('autoAdjustOverlap', limitData.maxOverlapSize));
        }

      } catch (error) {
        console.error('設定制限の読み込みに失敗しました:', error);
        showWarning(t('loadLimitsFailed'));
      } finally {
        setIsLoadingLimits(false);
      }
    };

    loadLimits();
  }, [isOpen, selectedEmbedding]);

  // Track isOpen state change, reset only on open
  const [prevOpen, setPrevOpen] = useState(false);

  // Initialize modal
  useEffect(() => {
    if (isOpen && !prevOpen) {
      // Execute initialization only when going from closed to open
      console.log('DEBUG: IndexingModalWithMode opening, files:', files);

      // Set default embedding model
      const enabledModels = embeddingModels.filter(m => m.isEnabled !== false);
      const validDefault = enabledModels.find(m => m.id === defaultEmbeddingId);

      if (validDefault) {
        setSelectedEmbedding(defaultEmbeddingId);
      } else if (enabledModels.length > 0) {
        setSelectedEmbedding(enabledModels[0].id);
      } else {
        setSelectedEmbedding('');
      }

      // Reset to defaults
      setChunkSize(200);
      setChunkOverlap(40);
      if (!isReconfiguring) {
        setMode('fast');
        setUserSelectedMode(false); // Reset user selection status
      }
      setModeRecommendation(null);
    }

    setPrevOpen(isOpen);
  }, [isOpen, prevOpen, defaultEmbeddingId, embeddingModels, isReconfiguring]);

  // Handle chunk size change
  const handleChunkSizeChange = (value: number) => {
    if (limits && value > limits.maxChunkSize) {
      showWarning(t('maxValueMsg', limits.maxChunkSize));
      setChunkSize(limits.maxChunkSize);
      return;
    }
    setChunkSize(value);

    // Auto-adjust overlap if it exceeds 50% of new chunk size
    if (chunkOverlap > value * 0.5) {
      setChunkOverlap(Math.floor(value * 0.5));
    }
  };

  // Handle overlap size change
  const handleChunkOverlapChange = (value: number) => {
    if (limits && value > limits.maxOverlapSize) {
      showWarning(t('maxValueMsg', limits.maxOverlapSize));
      setChunkOverlap(limits.maxOverlapSize);
      return;
    }

    // Check if it exceeds 50% of chunk size
    const maxOverlapByRatio = Math.floor(chunkSize * 0.5);
    if (value > maxOverlapByRatio) {
      showWarning(t('overlapRatioLimit', maxOverlapByRatio));
      setChunkOverlap(maxOverlapByRatio);
      return;
    }

    setChunkOverlap(value);
  };

  // Render limits info
  const renderLimitsInfo = () => {
    if (!limits || isLoadingLimits) {
      return null;
    }

    return (
      <div className="bg-blue-50 border border-blue-200 rounded-lg p-3 text-xs">
        <div className="flex items-center gap-2 mb-2 font-semibold text-blue-800">
          <Info className="w-4 h-4" />
          {t('modelLimitsInfo')}
        </div>
        <div className="grid grid-cols-2 gap-2 text-blue-700">
          <div>{t('model')}: <span className="font-medium">{limits.modelInfo.name}</span></div>
          <div>{t('maxChunkSize')}: <span className="font-medium">{limits.maxChunkSize} tokens</span></div>
          <div>{t('maxOverlapSize')}: <span className="font-medium">{limits.maxOverlapSize} tokens</span></div>
          <div>{t('maxBatchSize')}: <span className="font-medium">{limits.modelInfo.maxBatchSize}</span></div>
        </div>
        {limits.modelInfo.maxInputTokens > limits.maxChunkSize && (
          <div className="mt-1 text-blue-600 text-[10px]">
            ⚠️ {t('envLimitWeaker')}: {limits.maxChunkSize} &lt; {limits.modelInfo.maxInputTokens}
          </div>
        )}
      </div>
    );
  };

  // Render mode recommendation info
  const renderModeRecommendation = () => {
    if (!modeRecommendation || isLoadingRecommendation) {
      return null;
    }

    return (
      <div className="space-y-2 p-3 bg-purple-50 border border-purple-200 rounded-lg text-xs">
        <div className="font-semibold text-purple-800 flex items-center gap-2">
          <Target className="w-4 h-4" />
          {t('processingMode')}
        </div>
        <div className="text-purple-700">
          <strong>{t('recommendationReason')}:</strong> {t(modeRecommendation.reason, ...(modeRecommendation.reasonArgs || []))}
        </div>
        {modeRecommendation.warnings && modeRecommendation.warnings.length > 0 && (
          <div className="mt-1 space-y-1">
            {modeRecommendation.warnings.map((warning: string, idx: number) => (
              <div key={idx} className="text-purple-800 flex items-start gap-1">
                <AlertTriangle className="w-3 h-3 mt-0.5 flex-shrink-0" />
                <span>{t(warning as any)}</span>
              </div>
            ))}
          </div>
        )}
      </div>
    );
  };

  // Render current mode description
  const renderModeDescription = () => {
    if (mode === 'fast') {
      return (
        <div className="text-xs text-slate-600 bg-slate-50 p-2 rounded border border-slate-200">
          <div className="font-semibold text-slate-700 mb-1 flex items-center gap-1">
            <Zap className="w-3 h-3 text-yellow-600" />
            {t('fastModeFeatures')}
          </div>
          <ul className="list-disc list-inside space-y-0.5 text-[11px]">
            <li>{t('fastFeature1')}</li>
            <li>{t('fastFeature2')}</li>
            <li>{t('fastFeature3')}</li>
            <li>{t('fastFeature4')}</li>
            <li>{t('fastFeature5')}</li>
          </ul>
        </div>
      );
    }

    return (
      <div className="text-xs text-slate-600 bg-slate-50 p-2 rounded border border-slate-200">
        <div className="font-semibold text-slate-700 mb-1 flex items-center gap-1">
          <Target className="w-3 h-3 text-blue-600" />
          {t('preciseModeFeatures')}
        </div>
        <ul className="list-disc list-inside space-y-0.5 text-[11px]">
          <li>{t('preciseFeature1')}</li>
          <li>{t('preciseFeature2')}</li>
          <li>{t('preciseFeature3')}</li>
          <li>{t('preciseFeature4')}</li>
          <li>{t('preciseFeature5')}</li>
          <li>{t('preciseFeature6')}</li>
        </ul>
      </div>
    );
  };

  if (!isOpen) return null;

  return createPortal(
    <>
      <div
        className="fixed inset-0 z-[100] bg-black/50 backdrop-blur-sm transition-opacity"
        onClick={onClose}
      />
      <div className="fixed right-0 top-0 h-full w-full max-w-lg bg-white shadow-2xl z-[101] transform transition-transform duration-300 ease-in-out animate-in slide-in-from-right flex flex-col">

        {/* Header */}
        <div className="p-5 border-b border-slate-100 bg-slate-50 shrink-0">
          <div className="flex justify-between items-start">
            <div>
              <h2 className="text-lg font-bold text-slate-800 flex items-center gap-2">
                <Database className="w-5 h-5 text-blue-600" />
                {isReconfiguring ? t('reconfigureTitle') : t('indexingConfigTitle')}
              </h2>
              <p className="text-xs text-slate-500 mt-1">
                {isReconfiguring ? t('reconfigureDesc') : t('indexingConfigDesc')}
              </p>
            </div>
            <button
              onClick={(e) => {
                e.stopPropagation();
                onClose();
              }}
              className="p-2 hover:bg-slate-200 rounded-full transition-colors active:scale-90"
            >
              <X className="w-5 h-5 text-slate-500" />
            </button>
          </div>
        </div>

        <div className="flex-1 overflow-y-auto p-5 space-y-6">

          {/* Pending files - only show when there are files */}
          {files && files.length > 0 && (
            <div>
              <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
                <Files className="w-4 h-4 text-slate-500" />
                {t('pendingFiles')}
              </h3>
              <div className="space-y-1 max-h-32 overflow-y-auto bg-slate-50 rounded-lg p-2 border border-slate-200">
                {files.map((file, index) => (
                  <div key={index} className="text-xs text-slate-600 flex items-center justify-between py-1 px-2 hover:bg-white rounded transition-colors">
                    <span className="truncate flex-1">{file.name}</span>
                    <span className="text-slate-400 ml-2">{formatBytes(file.size)}</span>
                  </div>
                ))}
              </div>
            </div>
          )}

          {/* Processing mode selection */}
          {!isReconfiguring && (
            <div>
              <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
                <Target className="w-4 h-4 text-slate-500" />
                {t('processingMode')}
                {isLoadingRecommendation && <span className="text-xs text-blue-600 ml-2">{t('analyzingFile')}</span>}
              </h3>

              {/* Mode recommendation info */}
              {renderModeRecommendation()}

              {/* Mode selection */}
              <div className="grid grid-cols-2 gap-3 mt-3">
                {/* Fast Mode */}
                <button
                  onClick={() => {
                    setMode('fast');
                    setUserSelectedMode(true); // 标记用户手动选择
                  }}
                  className={`relative p-3 rounded-lg border-2 text-left transition-all ${mode === 'fast'
                    ? 'border-blue-500 bg-blue-50'
                    : 'border-slate-200 hover:border-slate-300'
                    }`}
                >
                  <div className="flex items-center gap-2 mb-2">
                    <Zap className="w-4 h-4 text-yellow-600" />
                    <span className="font-semibold text-sm">{t('fastMode')}</span>
                  </div>
                  <div className="text-xs text-slate-600 leading-relaxed">
                    {t('fastModeDesc')}
                  </div>
                  {mode === 'fast' && (
                    <div className="absolute top-2 right-2 text-blue-600">
                      <div className="w-2 h-2 bg-blue-600 rounded-full"></div>
                    </div>
                  )}
                </button>

                {/* Precise Mode */}
                <button
                  onClick={() => {
                    setMode('precise');
                    setUserSelectedMode(true); // 标记用户手动选择
                  }}
                  className={`relative p-3 rounded-lg border-2 text-left transition-all ${mode === 'precise'
                    ? 'border-purple-500 bg-purple-50'
                    : 'border-slate-200 hover:border-slate-300'
                    }`}
                >
                  <div className="flex items-center gap-2 mb-2">
                    <Target className="w-4 h-4 text-purple-600" />
                    <span className="font-semibold text-sm">{t('preciseMode')}</span>
                  </div>
                  <div className="text-xs text-slate-600 leading-relaxed">
                    {t('preciseModeDesc')}
                  </div>
                  {mode === 'precise' && (
                    <div className="absolute top-2 right-2 text-purple-600">
                      <div className="w-2 h-2 bg-purple-600 rounded-full"></div>
                    </div>
                  )}
                </button>
              </div>

              {/* Mode description */}
              <div className="mt-3">
                {renderModeDescription()}
              </div>
            </div>
          )}

          {/* Embedding model selection */}
          <div>
            <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
              <Layers className="w-4 h-4 text-slate-500" />
              {t('embeddingModel')}
            </h3>
            <select
              className="w-full text-sm border border-slate-300 rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 outline-none"
              value={selectedEmbedding}
              onChange={(e) => setSelectedEmbedding(e.target.value)}
            >
              <option value="">{t('pleaseSelect')}</option>
              {embeddingModels.filter(m => m.isEnabled !== false).map(model => (
                <option key={model.id} value={model.id}>
                  {model.name} ({model.modelId})
                </option>
              ))}
            </select>
          </div>

          {/* Chunk config */}
          <div>
            <h3 className="text-sm font-semibold text-slate-700 mb-2 flex items-center gap-2">
              <FileText className="w-4 h-4 text-slate-500" />
              {t('chunkConfig')}
            </h3>

            <div className="space-y-3">
              {/* Chunk size */}
              <div>
                <div className="flex justify-between mb-1 text-xs">
                  <span className="text-slate-600">{t('chunkSize')}</span>
                  <span className="font-mono font-semibold text-blue-600">{chunkSize}</span>
                </div>
                <input
                  type="range"
                  min="50"
                  max={limits?.maxChunkSize || 8191}
                  value={chunkSize}
                  onChange={(e) => handleChunkSizeChange(Number(e.target.value))}
                  className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
                  disabled={!selectedEmbedding || isLoadingLimits}
                />
                <div className="flex justify-between text-[10px] text-slate-400 mt-1">
                  <span>{t('min')}: 50</span>
                  <span>{t('max')}: {limits?.maxChunkSize || '-'}</span>
                </div>
              </div>

              {/* Overlap size */}
              <div>
                <div className="flex justify-between mb-1 text-xs">
                  <span className="text-slate-600">{t('chunkOverlap')}</span>
                  <span className="font-mono font-semibold text-blue-600">{chunkOverlap}</span>
                </div>
                <input
                  type="range"
                  min="0"
                  max={limits?.maxOverlapSize || 200}
                  value={chunkOverlap}
                  onChange={(e) => handleChunkOverlapChange(Number(e.target.value))}
                  className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-blue-600"
                  disabled={!selectedEmbedding || isLoadingLimits}
                />
                <div className="flex justify-between text-[10px] text-slate-400 mt-1">
                  <span>{t('min')}: 0</span>
                  <span>{t('max')}: {limits?.maxOverlapSize || '-'}</span>
                </div>
              </div>
            </div>
          </div>

          {isReconfiguring && renderLimitsInfo()}

          {/* Optimization tips */}
          {limits && (
            <div className="bg-amber-50 border border-amber-200 rounded-lg p-3 text-xs text-amber-800">
              <p className="font-medium mb-1">💡 {t('optimizationTips')}</p>
              <ul className="list-disc list-inside space-y-0.5 text-[11px]">
                {chunkSize > 800 && <li>{t('tipChunkTooLarge')}</li>}
                {chunkOverlap < chunkSize * 0.1 && <li>{t('tipOverlapSmall').replace('$1', `${Math.floor(chunkSize * 0.1)}`)}</li>}
                {chunkSize === limits.maxChunkSize && <li>{t('tipMaxValues')}</li>}
                {mode === 'precise' && <li>{t('tipPreciseCost')}</li>}
              </ul>
            </div>
          )}

        </div>

        {/* Footer buttons */}
        <div className="p-4 border-t border-slate-100 bg-slate-50 flex justify-end gap-2 shrink-0">
          <button
            onClick={(e) => {
              e.stopPropagation();
              onClose();
            }}
            className="px-4 py-2 text-sm text-slate-600 hover:bg-slate-200 rounded-lg transition-colors active:scale-95"
          >
            {t('cancel')}
          </button>
          <button
            onClick={() => {
              if (!selectedEmbedding) {
                showWarning(t('selectEmbeddingFirst'));
                return;
              }
              if (!isReconfiguring && mode === 'precise') {
                // Precise mode confirmation
                if (!confirm(t('confirmPreciseCost'))) {
                  return;
                }
              }
              onConfirm({
                chunkSize,
                chunkOverlap,
                embeddingModelId: selectedEmbedding,
                mode,
              });
            }}
            disabled={isLoadingLimits}
            className="px-4 py-2 text-sm bg-blue-600 text-white hover:bg-blue-700 rounded-lg shadow-sm flex items-center gap-2 transition-transform active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed"
          >
            <Database className="w-4 h-4" />
            {t('startProcessing')}
          </button>
        </div>
      </div>
    </>,
    document.body
  );
};

export default IndexingModalWithMode;
