import React from 'react';
import { Settings, X } from 'lucide-react';

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

export default function InsiderBlueprintModal({ isOpen, onClose }: InsiderBlueprintModalProps) {
  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 overflow-y-auto bg-slate-950/40 backdrop-blur-sm flex items-start justify-center p-4 md:p-10 animate-fade-in">
      <div className="bg-slate-900 border border-slate-800/80 rounded-3xl w-full max-w-4xl my-auto max-h-full flex flex-col overflow-hidden shadow-2xl relative text-slate-350">
        
        {/* 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-amber-400 to-yellow-400 bg-clip-text text-transparent flex items-center gap-2">
              <Settings className="w-5 h-5 text-amber-400" /> Insider Trades - 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">Insider Trade Ingestion &amp; Filters</h3>
            <p className="text-xs text-slate-400 mt-1">Operational details of corporate insider trade tracking.</p>
          </div>

          {/* Core Mechanics */}
          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div className="p-4 rounded-xl border border-slate-800/60 bg-slate-950/40 space-y-2">
              <h4 className="text-xs font-bold text-amber-400 uppercase tracking-wider font-mono">1. SEC Form 4 Tracking</h4>
              <p className="text-xs text-slate-400 leading-relaxed font-sans">
                Monitors corporate insider purchases and sales filed on SEC Form 4. Concentrates on high-ranking corporate officers (CEO, CFO, COO) and board directors, whose trading behaviors exhibit historical correlation with forward stock performance.
              </p>
            </div>
            
            <div className="p-4 rounded-xl border border-slate-800/60 bg-slate-950/40 space-y-2">
              <h4 className="text-xs font-bold text-amber-400 uppercase tracking-wider font-mono">2. Trade Size Filters</h4>
              <p className="text-xs text-slate-400 leading-relaxed font-sans">
                Filters transactions by absolute dollar size (prioritizing trades exceeding $100k) and percentage weight change relative to the insider's existing ownership stake. This helps eliminate routine stock option exercises and automated sales.
              </p>
            </div>

            <div className="p-4 rounded-xl border border-slate-800/60 bg-slate-950/40 space-y-2">
              <h4 className="text-xs font-bold text-amber-400 uppercase tracking-wider font-mono">3. DEV_MODE Interception</h4>
              <p className="text-xs text-slate-400 leading-relaxed font-sans">
                When the offline shield is active, the `/api/insider` route short-circuits live FMP calls and serves a high-fidelity local database file to guarantee zero API consumption costs.
              </p>
            </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-amber-400 font-semibold">Insider Trades Ledger:</strong> Displays raw trade data: Tickers, Insider Names, Roles (CEO, Director, etc.), Buy/Sell transaction types, trade dates, shares traded, prices per share, and calculated total transaction values.
              </p>
              <p>
                <strong className="text-amber-400 font-semibold">API Layer Status Badge:</strong> Displays the status in the top control bar, showing `🟢 LIVE API` or `🟡 DEV-ARCHIV AKTIV` depending on the environment shield.
              </p>
            </div>
          </div>

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