// screens-kiosk.jsx — 게이트 키오스크(대형 스크린) 스캐너 + 분석

// ═══════════════════════════════════════════════════════════════
// 키오스크 스캐너 — 본관 정문 / 식당 입구 등 대형 디스플레이
// ═══════════════════════════════════════════════════════════════
function KioskScanner({ state = 'success' }) {
  return (
    <div style={{ width: '100%', height: '100%', background: '#15140F', color: '#EFEBE0', fontFamily: 'var(--font-sans)', position: 'relative', overflow: 'hidden', display: 'flex' }}>
      {/* Left: scan area */}
      <div style={{ flex: 1, padding: 56, display: 'flex', flexDirection: 'column', position: 'relative' }}>
        {/* header */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 'auto' }}>
          <div>
            <Brandmark size={28} subtle />
          </div>
          <div style={{ textAlign: 'right' }}>
            <div className="serif" style={{ fontSize: 22, fontWeight: 500, color: '#EFEBE0' }}>본관 정문</div>
            <div className="mono tabular" style={{ fontSize: 14, color: '#807A6F', marginTop: 4 }}>14:32 · 2026.05.08</div>
          </div>
        </div>

        {/* scan center */}
        <div style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', flexDirection: 'column', gap: 32 }}>
          <div style={{ position: 'relative', width: 320, height: 320 }}>
            {/* outer ring */}
            <div style={{ position: 'absolute', inset: 0, borderRadius: 24, border: '1.5px solid #2D2A24' }} />
            {/* corner brackets celadon */}
            {['tl','tr','bl','br'].map(k => (
              <div key={k} style={{
                position: 'absolute', width: 56, height: 56,
                borderColor: state==='denied'? '#D87968' : '#8FB1A4', borderStyle: 'solid', borderWidth: 0,
                ...(k==='tl'?{top:-2,left:-2,borderTopWidth:3,borderLeftWidth:3,borderTopLeftRadius:24}:{}),
                ...(k==='tr'?{top:-2,right:-2,borderTopWidth:3,borderRightWidth:3,borderTopRightRadius:24}:{}),
                ...(k==='bl'?{bottom:-2,left:-2,borderBottomWidth:3,borderLeftWidth:3,borderBottomLeftRadius:24}:{}),
                ...(k==='br'?{bottom:-2,right:-2,borderBottomWidth:3,borderRightWidth:3,borderBottomRightRadius:24}:{}),
              }} />
            ))}
            {/* ambient inner glow */}
            <div style={{ position: 'absolute', inset: 12, borderRadius: 16, background: state==='denied' ? 'radial-gradient(ellipse, rgba(216,121,104,0.08) 0%, transparent 70%)' : 'radial-gradient(ellipse, rgba(143,177,164,0.08) 0%, transparent 70%)' }} />
            {/* QR placeholder showing scan */}
            <div style={{ position: 'absolute', inset: 38, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
              {state === 'success' ? (
                <div style={{ width: 140, height: 140, borderRadius: 100, background: '#8FB1A4', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 0 80px rgba(143,177,164,0.4)' }}>
                  <Icon name="check" size={64} color="#15140F" />
                </div>
              ) : state === 'denied' ? (
                <div style={{ width: 140, height: 140, borderRadius: 100, background: '#D87968', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 0 80px rgba(216,121,104,0.4)' }}>
                  <Icon name="x" size={64} color="#15140F" />
                </div>
              ) : (
                <QRBlock size={200} seed="KIOSK-SCAN" dark="#EFEBE0" />
              )}
            </div>
          </div>

          <div style={{ textAlign: 'center', minHeight: 96 }}>
            {state === 'success' && <>
              <div className="serif" style={{ fontSize: 44, fontWeight: 500, color: '#EFEBE0', letterSpacing: '-0.02em', lineHeight: 1.1 }}>환영합니다, 김도현님</div>
              <div style={{ fontSize: 18, color: '#A6A095', marginTop: 12 }}>5F 회의실 304 · 박정민 책임 미팅</div>
              <div style={{ marginTop: 14, display: 'inline-flex', alignItems: 'center', gap: 8, padding: '6px 16px', background: 'rgba(143,177,164,0.12)', border: '0.5px solid rgba(143,177,164,0.4)', borderRadius: 100, fontSize: 13, color: '#8FB1A4' }}>
                <Icon name="arrow" size={13} color="#8FB1A4" />
                오른쪽 게이트로 입장하세요
              </div>
            </>}
            {state === 'denied' && <>
              <div className="serif" style={{ fontSize: 40, fontWeight: 500, color: '#EFEBE0' }}>출입할 수 없습니다</div>
              <div style={{ fontSize: 16, color: '#A6A095', marginTop: 12 }}>QR이 만료되었거나 권한이 없는 게이트입니다.<br/>안내 데스크에 문의해 주세요.</div>
            </>}
            {state === 'idle' && <>
              <div className="serif" style={{ fontSize: 40, fontWeight: 500, color: '#EFEBE0' }}>QR을 비춰주세요</div>
              <div style={{ fontSize: 16, color: '#A6A095', marginTop: 12 }}>휴대폰 또는 출입카드의 QR을 화면 가운데에 맞춰주세요</div>
            </>}
          </div>
        </div>

        {/* footer help */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 'auto', paddingTop: 24 }}>
          <div style={{ display: 'flex', gap: 24, fontSize: 13, color: '#807A6F' }}>
            <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}><span style={{ width: 6, height: 6, borderRadius: 100, background: '#8FB1A4' }} />시스템 정상</span>
            <span style={{ display: 'flex', alignItems: 'center', gap: 8 }}><Icon name="shield" size={13} color="#807A6F" />SSL · 동적QR</span>
          </div>
          <div style={{ display: 'flex', gap: 14, fontSize: 13, color: '#A6A095' }}>
            <span>안내데스크 · 02-1588-0808</span>
          </div>
        </div>
      </div>

      {/* Right: live feed */}
      <aside style={{ width: 360, background: '#1B1916', borderLeft: '0.5px solid #2D2A24', padding: '40px 28px', display: 'flex', flexDirection: 'column' }}>
        <div style={{ fontSize: 11, color: '#807A6F', letterSpacing: '0.2em', textTransform: 'uppercase', marginBottom: 18 }}>최근 입장</div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 1, background: '#2D2A24', borderRadius: 10, overflow: 'hidden' }}>
          {[
            { t:'14:32', name:'김도현', dest:'5F 회의실 304', ok:true },
            { t:'14:31', name:'이지훈', dest:'IT팀 · 사무실', ok:true },
            { t:'14:30', name:'박서연', dest:'5F 라운지', ok:true },
            { t:'14:29', name:'미인증', dest:'후문 · 거부', ok:false },
            { t:'14:28', name:'정유나', dest:'4F 인사팀', ok:true },
            { t:'14:27', name:'최도윤', dest:'OUT · 정문', ok:true },
          ].map((r, i) => (
            <div key={i} style={{ display: 'grid', gridTemplateColumns: '40px 1fr 14px', gap: 12, padding: '12px 14px', background: '#1B1916', alignItems: 'center' }}>
              <span className="mono" style={{ fontSize: 11, color: '#807A6F' }}>{r.t}</span>
              <div style={{ minWidth: 0 }}>
                <div style={{ fontSize: 13, color: r.ok ? '#EFEBE0' : '#D87968', fontWeight: 500 }}>{r.name}</div>
                <div style={{ fontSize: 10.5, color: '#807A6F', marginTop: 1 }}>{r.dest}</div>
              </div>
              {r.ok ? <Icon name="check" size={12} color="#8FB1A4" /> : <Icon name="x" size={12} color="#D87968" />}
            </div>
          ))}
        </div>

        <div style={{ marginTop: 'auto', paddingTop: 24, borderTop: '0.5px solid #2D2A24', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 1, background: '#2D2A24', borderRadius: 8, overflow: 'hidden' }}>
          {[
            { l:'금일 출입', v:'1,847', u:'건' },
            { l:'현재 체류', v:'248', u:'명' },
            { l:'오류', v:'2', u:'건' },
            { l:'평균 처리', v:'0.8', u:'초' },
          ].map((s, i) => (
            <div key={i} style={{ background: '#1B1916', padding: '14px 14px' }}>
              <div style={{ fontSize: 10.5, color: '#807A6F', letterSpacing: '0.04em' }}>{s.l}</div>
              <div style={{ display: 'flex', alignItems: 'baseline', gap: 4, marginTop: 4 }}>
                <span className="serif tabular" style={{ fontSize: 22, color: '#EFEBE0' }}>{s.v}</span>
                <span style={{ fontSize: 11, color: '#807A6F' }}>{s.u}</span>
              </div>
            </div>
          ))}
        </div>
      </aside>
    </div>
  );
}

// ═══════════════════════════════════════════════════════════════
// 게이트/구역 관리
// ═══════════════════════════════════════════════════════════════
function GatesAdmin() {
  const gates = [
    { id:'GT-01', name:'본관 정문', kind:'turnstile', state:'정상', count:128, devices:2, sec:'동적QR + 안면', tone:'approved' },
    { id:'GT-02', name:'후문', kind:'turnstile', state:'정상', count:34, devices:1, sec:'동적QR', tone:'approved' },
    { id:'GT-03', name:'B1 식당 입구', kind:'kiosk', state:'정상', count:847, devices:3, sec:'정적QR (일일권)', tone:'approved' },
    { id:'GT-04', name:'주차장 차단기', kind:'gate', state:'점검중', count:92, devices:1, sec:'차량번호 + QR', tone:'pending' },
    { id:'GT-05', name:'5F 회의실 304', kind:'doorlock', state:'정상', count:12, devices:1, sec:'NFC + QR', tone:'approved' },
    { id:'GT-06', name:'B2 서버실', kind:'doorlock', state:'정상', count:3, devices:2, sec:'2FA + QR', tone:'approved' },
    { id:'GT-07', name:'옥상 출입구', kind:'doorlock', state:'잠금', count:0, devices:1, sec:'관리자 한정', tone:'denied' },
  ];
  return (
    <div style={{ width: '100%', height: '100%', display: 'flex', background: 'var(--paper-1)', color: 'var(--ink-0)', fontFamily: 'var(--font-sans)' }}>
      <AdminSidebar active="gates" />
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0 }}>
        <AdminTopbar title="게이트 · 채널 관리" subtitle="7개 게이트 · 11개 단말기"
          actions={<button style={{ background: 'var(--ink-0)', color: 'var(--paper-0)', border: 'none', padding: '8px 14px', borderRadius: 7, fontSize: 12, fontWeight: 500, cursor: 'pointer', fontFamily: 'inherit', display: 'flex', alignItems: 'center', gap: 6 }}><Icon name="plus" size={13} />게이트 추가</button>} />

        <div style={{ flex: 1, padding: '28px 48px', display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 24, overflow: 'hidden' }}>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
              <div className="serif" style={{ fontSize: 16, fontWeight: 500 }}>게이트 목록</div>
              <div style={{ display: 'flex', gap: 6 }}>
                <SegBtn active>전체</SegBtn>
                <SegBtn>턴스타일</SegBtn>
                <SegBtn>도어락</SegBtn>
                <SegBtn>키오스크</SegBtn>
              </div>
            </div>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              {gates.map(g => (
                <div key={g.id} style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: '16px 20px', display: 'grid', gridTemplateColumns: '24px 1fr auto auto auto', gap: 16, alignItems: 'center' }}>
                  <span style={{ width: 8, height: 8, borderRadius: 100, background: g.tone === 'approved' ? 'var(--celadon)' : g.tone === 'pending' ? 'var(--ochre)' : 'var(--clay)' }} />
                  <div>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                      <span style={{ fontSize: 13.5, fontWeight: 500, color: 'var(--ink-0)' }}>{g.name}</span>
                      <span className="mono" style={{ fontSize: 10.5, color: 'var(--ink-3)' }}>{g.id}</span>
                    </div>
                    <div style={{ fontSize: 11, color: 'var(--ink-3)', marginTop: 2 }}>{g.sec} · {g.devices}대 단말기</div>
                  </div>
                  <div style={{ textAlign: 'right' }}>
                    <div className="tabular" style={{ fontSize: 14, fontWeight: 500, color: 'var(--ink-0)' }}>{g.count}</div>
                    <div style={{ fontSize: 10, color: 'var(--ink-3)' }}>오늘</div>
                  </div>
                  <Pill tone={g.tone} dot soft>{g.state}</Pill>
                  <Icon name="chevron" size={14} color="var(--ink-3)" />
                </div>
              ))}
            </div>
          </div>

          {/* Right: floor map */}
          <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: 22, display: 'flex', flexDirection: 'column', gap: 14 }}>
            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
              <div className="serif" style={{ fontSize: 14, fontWeight: 500 }}>1층 평면도</div>
              <div style={{ display: 'flex', gap: 4 }}>
                <SegBtn>B1</SegBtn>
                <SegBtn active>1F</SegBtn>
                <SegBtn>5F</SegBtn>
              </div>
            </div>
            <div style={{ flex: 1, position: 'relative', background: 'var(--paper-1)', borderRadius: 10, border: '0.5px dashed var(--paper-stroke)', minHeight: 360 }}>
              {/* schematic floorplan */}
              <svg viewBox="0 0 400 320" style={{ width: '100%', height: '100%', display: 'block' }}>
                {/* outer wall */}
                <rect x="20" y="20" width="360" height="280" fill="none" stroke="var(--ink-2)" strokeWidth="1.5" />
                {/* lobby */}
                <rect x="120" y="100" width="160" height="120" fill="var(--paper-2)" stroke="var(--paper-stroke)" strokeWidth="1" strokeDasharray="3 3" />
                <text x="200" y="165" textAnchor="middle" fontSize="11" fill="var(--ink-2)" fontFamily="var(--font-sans)">로비</text>
                {/* rooms */}
                <rect x="20" y="20" width="100" height="80" fill="none" stroke="var(--paper-stroke)" />
                <text x="70" y="65" textAnchor="middle" fontSize="9" fill="var(--ink-3)">안내데스크</text>
                <rect x="280" y="20" width="100" height="80" fill="none" stroke="var(--paper-stroke)" />
                <text x="330" y="65" textAnchor="middle" fontSize="9" fill="var(--ink-3)">우편실</text>
                <rect x="280" y="220" width="100" height="80" fill="none" stroke="var(--paper-stroke)" />
                <text x="330" y="265" textAnchor="middle" fontSize="9" fill="var(--ink-3)">엘리베이터</text>
                {/* gates as dots */}
                {[
                  { x:200, y:300, label:'정문', state:'ok' },
                  { x:200, y:20, label:'후문', state:'ok' },
                  { x:280, y:260, label:'EV', state:'ok' },
                  { x:20, y:260, label:'주차', state:'pending' },
                ].map((p, i) => (
                  <g key={i}>
                    <circle cx={p.x} cy={p.y} r="9" fill={p.state==='ok'?'var(--celadon)':'var(--ochre)'} stroke="var(--paper-0)" strokeWidth="2" />
                    <circle cx={p.x} cy={p.y} r="14" fill="none" stroke={p.state==='ok'?'var(--celadon)':'var(--ochre)'} strokeWidth="1" opacity="0.4" />
                  </g>
                ))}
              </svg>
            </div>
            <div style={{ display: 'flex', gap: 14, fontSize: 11, color: 'var(--ink-2)' }}>
              <span style={{ display: 'flex', alignItems: 'center', gap: 6 }}><span style={{ width: 7, height: 7, borderRadius: 100, background: 'var(--celadon)' }} />정상 (5)</span>
              <span style={{ display: 'flex', alignItems: 'center', gap: 6 }}><span style={{ width: 7, height: 7, borderRadius: 100, background: 'var(--ochre)' }} />점검 (1)</span>
              <span style={{ display: 'flex', alignItems: 'center', gap: 6 }}><span style={{ width: 7, height: 7, borderRadius: 100, background: 'var(--clay)' }} />잠금 (1)</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { KioskScanner, GatesAdmin });
