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

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

export default function TechBlueprintModal({ isOpen, onClose }: TechBlueprintModalProps) {
  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-900/85 backdrop-blur-md p-4 sm:p-6 md:p-8 animate-fade-in">
      <div className="bg-slate-900 border border-slate-800/80 rounded-3xl w-full max-w-5xl h-[85vh] flex flex-col overflow-hidden shadow-2xl relative text-slate-355">
        
        {/* Modal Header */}
        <div className="flex justify-between items-center px-6 py-4 bg-slate-950/45 border-b border-slate-800/60">
          <div>
            <h2 className="text-base font-bold bg-gradient-to-r from-teal-400 to-indigo-400 bg-clip-text text-transparent flex items-center gap-2">
              <Settings className="w-5 h-5 text-teal-400" /> AI &amp; Tech Silo - Operational Blueprint
            </h2>
            <p className="text-[10px] text-slate-500 font-mono">System User Manual &amp; Interface Mechanics</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 p-2 rounded-xl transition-all cursor-pointer flex items-center justify-center"
            aria-label="Close modal"
          >
            <X className="w-4 h-4" />
          </button>
        </div>

        {/* Modal Body */}
        <div className="flex-1 overflow-y-auto p-6 sm:p-8 space-y-8 text-slate-350 scrollbar-thin">
          
          <div className="border-b border-slate-800/80 pb-3">
            <h3 className="text-base font-bold text-slate-200">CapEx Cycle &amp; Infrastructure Indicators</h3>
            <p className="text-xs text-slate-400 mt-1">Operational details of tech overcapacity diagnosis, segment monetization, and supply-chain metrics.</p>
          </div>

          {/* Core Mechanics */}
          <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
            <div className="p-5 rounded-2xl border border-slate-800/60 bg-slate-950/40 space-y-3 flex flex-col">
              <h4 className="text-xs font-bold text-teal-400 uppercase tracking-wider font-mono">1. Monetization Gap &amp; ROI-to-CapEx</h4>
              <div className="text-xs text-slate-400 leading-relaxed font-sans space-y-2 flex-1">
                <p>
                  Monitors segment revenues (Azure, AWS, Google Cloud, Meta Family of Apps) against capital spending growth. The **Monetization Gap** is defined as the cloud-segment revenue growth percentage minus the absolute CapEx growth percentage:
                </p>
                <BlockMath math="G_{\text{monetization}} = \Delta \text{Rev}_{\text{segment}} - \Delta \text{CapEx}" />
                <p>
                  A negative monetization gap (<InlineMath math="G_{\text{monetization}} < 0" />) signals that cloud revenues are growing slower than capital investments, indicating potential write-down risks.
                </p>
              </div>
            </div>
            
            <div className="p-5 rounded-2xl border border-slate-800/60 bg-slate-950/40 space-y-3 flex flex-col">
              <h4 className="text-xs font-bold text-teal-400 uppercase tracking-wider font-mono">2. Supply-Chain Velocity Index</h4>
              <div className="text-xs text-slate-400 leading-relaxed font-sans space-y-2 flex-1">
                <p>
                  Data origins are automated parsers indexing SEC 10-Q filings for forward capital allocations. The **Supply-Chain Velocity Index** maps the buyers' forward purchase commitments against the hardware supplier's (Nvidia) spot inventory:
                </p>
                <BlockMath math="V_{\text{sc}} = \frac{\sum_{j \in \text{Buyers}} \text{Purchase Obligations}_{j, t}}{\text{Inventory}_{\text{supplier}, t}}" />
                <p>
                  This compares forward commitments to the supplier's balance sheet inventory levels to diagnose macro overcapacity and demand corrections 3-6 months in advance.
                </p>
              </div>
            </div>

            <div className="p-5 rounded-2xl border border-slate-800/60 bg-slate-950/40 space-y-3 flex flex-col">
              <h4 className="text-xs font-bold text-teal-400 uppercase tracking-wider font-mono">3. Cluster Leverage (D/E &amp; CapEx/Dep)</h4>
              <div className="text-xs text-slate-400 leading-relaxed font-sans space-y-2 flex-1">
                <p>
                  Monitors CapEx-to-depreciation ratios alongside Debt-to-Equity changes:
                </p>
                <BlockMath math="R_{\text{capex-dep}} = \frac{\text{CapEx}}{\text{Depreciation}}" />
                <p>
                  Ratios exceeding <InlineMath math="3\times" /> indicate hyper-aggressive server capacity expansion. When this occurs alongside rising Debt-to-Equity ratios, it indicates that cluster construction is funded by leverage, exposing firms to massive future amortization drag.
                </p>
              </div>
            </div>
          </div>

          {/* Interface Specifications */}
          <div className="space-y-3">
            <h3 className="text-sm font-bold text-slate-200 border-b border-slate-800 pb-2">Interface Layout &amp; Mechanics</h3>
            <div className="text-xs text-slate-400 space-y-3 leading-relaxed">
              <p>
                <strong className="text-teal-400 font-semibold">Sloan Accrual Card &amp; Indicators:</strong> Integrates the Sloan Ratio indicator at the top of the silo dashboard, classifying reported revenues into Safe or Anomaly regimes.
              </p>
              <p>
                <strong className="text-teal-400 font-semibold">Silo Dashboard Sparklines:</strong> Displays quarterly trajectories for MSFT, GOOGL, META, AMZN, and AMD segment revenue and capital growth rates.
              </p>
            </div>
          </div>

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