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

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

export default function MacroMathModal({ isOpen, onClose }: MacroMathModalProps) {
  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-purple-400 to-indigo-400 bg-clip-text text-transparent flex items-center gap-2">
              <BookOpen className="w-5 h-5 text-purple-400" /> Macroeconomics & Liquidity - 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">6. Macroeconomic Indicators & Credit Vault</h3>
              <p className="text-xs text-slate-400 mt-1">Details structural curves, monetary flows, and historical surprise indices.</p>
            </div>

            {/* Section A: Yield Curve */}
            <div className="space-y-3">
              <h4 className="text-xs font-bold text-purple-400 uppercase tracking-wider font-mono">A. Yield Curve Spread Inversion Dynamics</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                Calculates the duration spread between short-term and long-term government yield rates. A negative spread represents structural inversion, often preceding a recession:
              </p>
              <div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2 space-y-4">
                <div>
                  <p className="text-xs text-slate-400 mb-1">2S10S Yield Curve Spread:</p>
                  <BlockMath math="\\text{Spread}_{2S10S} = Y_{10Y} - Y_{2Y}" />
                  <p className="text-[10px] text-slate-550 mt-2 font-mono leading-relaxed">
                    where:
                    <br />
                    - <InlineMath math="Y_{10Y}" /> is the yield rate of the 10-Year Treasury Bond.
                    <br />
                    - <InlineMath math="Y_{2Y}" /> is the yield rate of the 2-Year Treasury Bond.
                  </p>
                </div>
              </div>
            </div>

            {/* Section B: Surprise Index */}
            <div className="space-y-3">
              <h4 className="text-xs font-bold text-purple-400 uppercase tracking-wider font-mono">B. Surprise Index Standardized Deviation</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                Measures how far an economic release deviates from general consensus expectations, scaled by the historical standard deviation of surprises:
              </p>
              <div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2 space-y-4">
                <div>
                  <p className="text-xs text-slate-400 mb-1">Standardized Surprise Score:</p>
                  <BlockMath math="\\text{Surprise}_t = \\frac{\\text{Actual}_t - \\text{Consensus}_t}{\\sigma_{\\text{surprise}}}" />
                  <p className="text-[10px] text-slate-550 mt-2 font-mono leading-relaxed">
                    where:
                    <br />
                    - <InlineMath math="\\text{Actual}_t" /> is the released value for the economic indicator.
                    <br />
                    - <InlineMath math="\\text{Consensus}_t" /> is the median consensus forecast.
                    <br />
                    - <InlineMath math="\\sigma_{\\text{surprise}}" /> is the historical standard deviation of forecast errors.
                  </p>
                </div>
              </div>
            </div>

            {/* Section C: Net Liquidity */}
            <div className="space-y-3">
              <h4 className="text-xs font-bold text-purple-400 uppercase tracking-wider font-mono">C. Central Bank Net Liquidity Proxy</h4>
              <p className="text-xs leading-relaxed text-slate-400">
                Calculates the net USD liquidity circulating in the financial system by subtracting treasury reserves and central bank operations:
              </p>
              <div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2 space-y-4">
                <div>
                  <p className="text-xs text-slate-400 mb-1">Federal Reserve Net Liquidity Equation:</p>
                  <BlockMath math="\\text{Net Liquidity}_t = A_{\\text{Fed}, t} - \\text{TGA}_t - \\text{RRP}_t" />
                  <p className="text-[10px] text-slate-550 mt-2 font-mono leading-relaxed">
                    where:
                    <br />
                    - <InlineMath math="A_{\\text{Fed}, t}" /> is the total Federal Reserve assets (balance sheet volume).
                    <br />
                    - <InlineMath math="\\text{TGA}_t" /> is the Treasury General Account balance at the Fed.
                    <br />
                    - <InlineMath math="\\text{RRP}_t" /> is the Reverse Repo facility usage volume.
                  </p>
                </div>
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>
  );
}
