import React from 'react';
import { BookOpen } from 'lucide-react';
import 'katex/dist/katex.min.css';
import { BlockMath, InlineMath } from 'react-katex';

interface CryptoMathModalProps {
  isOpen: boolean;
  onClose: () => void;
}

export default function CryptoMathModal({ isOpen, onClose }: CryptoMathModalProps) {
  React.useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.key === 'Escape') {
        onClose();
      }
    };
    if (isOpen) {
      window.addEventListener('keydown', handleKeyDown);
    }
    return () => {
      window.removeEventListener('keydown', handleKeyDown);
    };
  }, [isOpen, onClose]);

  if (!isOpen) return null;

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/85 backdrop-blur-md p-4 sm:p-6 md:p-8">
      <div className="bg-slate-900 border border-slate-800/80 rounded-3xl w-full max-w-4xl h-[80vh] flex flex-col overflow-hidden shadow-2xl relative text-slate-300">
        
        {/* Modal Header */}
        <div className="flex justify-between items-center px-6 py-4 bg-slate-950/40 border-b border-slate-800/60">
          <div>
            <h2 className="text-base font-bold bg-gradient-to-r from-cyan-400 to-sky-400 bg-clip-text text-transparent flex items-center gap-2">
              <BookOpen className="w-5 h-5 text-cyan-400" /> Crypto Bayesian Markov - Math & Logic Specification
            </h2>
            <p className="text-[10px] text-slate-500 font-mono">Institutional Specification Manual</p>
          </div>
          <button
            onClick={onClose}
            className="text-slate-400 hover:text-slate-200 bg-slate-950/50 border border-slate-800 hover:border-slate-700 px-3 py-1.5 rounded-lg text-xs font-semibold font-mono transition-all cursor-pointer"
          >
            Schließen (ESC)
          </button>
        </div>

        {/* Modal Body */}
        <div className="flex-1 overflow-y-auto p-6 sm:p-8 space-y-6 text-slate-300 scrollbar-thin">
          <div className="space-y-6">
            <div className="border-b border-slate-800/80 pb-3">
              <h3 className="text-base font-bold text-slate-200">4. Crypto Bayesian Markov Engine</h3>
              <p className="text-xs text-slate-400 mt-1">Models momentum regimes and updates transition probabilities using on-chain alpha inputs.</p>
            </div>

            <div className="space-y-3">
              <h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">A. Markov Chain State Space</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                The asset return state space is mapped into 3 momentum regimes:
              </p>
              <div className="grid grid-cols-3 gap-3 text-xs text-slate-400 font-mono text-center">
                <div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
                  <span className="block text-rose-400 font-bold">State 1 (S1)</span>
                  <span>Bearish Squeeze / Crackdown</span>
                </div>
                <div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
                  <span className="block text-slate-300 font-bold">State 2 (S2)</span>
                  <span>Consolidation / Mean Reversion</span>
                </div>
                <div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
                  <span className="block text-emerald-400 font-bold">State 3 (S3)</span>
                  <span>Parabolic Bull Run</span>
                </div>
              </div>
            </div>

            <div className="space-y-3">
              <h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">B. Transition Matrix (P)</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                Calculates transition probabilities over rolling 90-day return vectors:
              </p>
              <div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
                <BlockMath math="P = \begin{bmatrix} p_{11} & p_{12} & p_{13} \\ p_{21} & p_{22} & p_{23} \\ p_{31} & p_{32} & p_{33} \end{bmatrix}" />
                <p className="text-[11px] text-slate-400 font-mono mt-2 text-center">
                  where <InlineMath math="p_{ij} = P(X_{t+1} = S_j \mid X_t = S_i)" /> represents the frequency probability of moving from State i to State j.
                </p>
              </div>
            </div>

            <div className="space-y-3">
              <h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">C. Bayesian Update Engine</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                When external alpha inputs (e.g. Funding Rate anomalies, Whale inflows) occur, state probabilities are updated using Bayes' theorem:
              </p>
              <div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
                <BlockMath math="P(S_i \mid \text{Alpha}) = \frac{P(\text{Alpha} \mid S_i) \times P(S_i)}{\sum_{j=1}^3 P(\text{Alpha} \mid S_j) \times P(S_j)}" />
                <p className="text-[11px] text-slate-400 mt-2 font-mono leading-relaxed">
                  Where:<br/>
                  - <InlineMath math="P(S_i)" /> is the prior state probability from the Markov transition matrix.<br/>
                  - <InlineMath math="P(\text{Alpha} \mid S_i)" /> is the conditional likelihood of observing this whale spike / funding squeeze in State i.
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
