/* rx-session.jsx — 세션 상태 관리
   requires: window.AuthContext (rx-auth.jsx), window.rxHelpers (rx-core.jsx)
   exports:  useRxSession, DEPTS, ComingSoon */

const DEPTS = [
  { id: 'all', label: '통합', enabled: true },
  { id: 'CV', label: '순환기', enabled: true },
  { id: 'PM', label: '호흡기', enabled: false },
  { id: 'ED', label: '내분비', enabled: false },
];

function ComingSoon({ label }) {
  return (
    <div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 12, padding: 40, textAlign: 'center', color: RX.faint }}>
      <svg width="40" height="40" viewBox="0 0 24 24" fill="none"><rect x="3.5" y="5" width="17" height="14" rx="2.5" stroke={RX.line} strokeWidth="1.6" /><path d="M7 9.5h10M7 13h6" stroke={RX.line} strokeWidth="1.6" strokeLinecap="round" /></svg>
      <div style={{ fontSize: 15, fontWeight: 700, color: RX.sub }}>{label}</div>
      <div style={{ fontSize: 12.5, lineHeight: 1.6, maxWidth: 220 }}>이 섹션은 준비 중입니다. 현재는 <b style={{ color: RX.teal }}>처방전 분석</b> 학습 기능을 먼저 제공합니다.</div>
    </div>
  );
}

function useRxSession(initialKey = 'CV_HyperT') {
  const { plan } = React.useContext(window.AuthContext || {});
  const visibleDepts = window.getVisibleDepts(plan);

  const [tab, setTab] = React.useState('rx');
  const [subspec, setSubspec] = React.useState('CV');
  const [diseaseKey, setKey] = React.useState(initialKey);
  const [caseIdx, setCaseIdx] = React.useState(0);
  const [drug, setDrug] = React.useState(null);
  const [revealed, setRevealed] = React.useState(false);
  const [customPattern, setCustomPattern] = React.useState(null);

  React.useEffect(() => {
    if (!visibleDepts.some(d => d.id === subspec && d.enabled)) {
      setSubspec('CV');
      setKey('__all');
      setCaseIdx(0);
      setRevealed(false);
      setDrug(null);
    }
  }, [plan]);

  const scoped = rxHelpers.scopedPatterns(subspec, diseaseKey, plan);
  const safe = Math.max(0, Math.min(caseIdx, scoped.length - 1));
  const pattern = customPattern || (scoped[safe] || scoped[0]).pattern;

  const onSubspec = (sp) => {
    const list = rxHelpers.diseaseList(sp, plan);
    const valid = diseaseKey === '__all' || list.some(d => d.key === diseaseKey);
    setSubspec(sp);
    if (!valid) { setKey('__all'); setCaseIdx(0); }
    setRevealed(false); setDrug(null); setCustomPattern(null);
  };
  const onDisease = (k) => { setKey(k); setCaseIdx(0); setRevealed(false); setDrug(null); setCustomPattern(null); };
  const onCase = (i) => { setCaseIdx(i); setRevealed(false); setDrug(null); setCustomPattern(null); };
  const onRandom = () => {
    if (diseaseKey !== '__all') {
      const patterns = rxHelpers.scopedPatterns(subspec, diseaseKey, plan);
      if (patterns.length > 0) {
        setCaseIdx(Math.floor(Math.random() * patterns.length));
      }
    } else {
      const r = rxHelpers.weightedRandom(subspec, plan);
      setCaseIdx(rxHelpers.scopedIdxOf(subspec, '__all', r.patternId, plan));
    }
    setRevealed(false); setDrug(null); setCustomPattern(null);
  };

  return { tab, setTab, subspec, onSubspec, diseaseKey, caseIdx: safe, pattern, customPattern, setCustomPattern, drug, setDrug, revealed, setRevealed, onDisease, onCase, onRandom };
}

Object.assign(window, { useRxSession, DEPTS, ComingSoon });
