import { useState, useEffect } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import Layout from './components/Layout';
import StartupCheck from './components/StartupCheck';
import DebugConsole from './components/DebugConsole';
import Home from './pages/Home';
import Join from './pages/Join';
import Create from './pages/Create';
import Session from './pages/Session';
import Sign from './pages/Sign';
import Settings from './pages/Settings';
// Co-Sign 页面
import CoSignCreate from './pages/CoSignCreate';
import CoSignJoin from './pages/CoSignJoin';
import CoSignSession from './pages/CoSignSession';

function App() {
  const [startupComplete, setStartupComplete] = useState(false);
  const [showDebugConsole, setShowDebugConsole] = useState(false);

  // 监听键盘快捷键 Ctrl+Shift+D 打开调试窗口
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.ctrlKey && e.shiftKey && e.key === 'D') {
        e.preventDefault();
        setShowDebugConsole(prev => !prev);
      }
    };

    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, []);

  // 显示启动检测页面
  if (!startupComplete) {
    return <StartupCheck onComplete={() => setStartupComplete(true)} />;
  }

  return (
    <>
      <Layout>
        <Routes>
          <Route path="/" element={<Home />} />
          {/* Keygen 路由 */}
          <Route path="/join" element={<Join />} />
          <Route path="/join/:inviteCode" element={<Join />} />
          <Route path="/create" element={<Create />} />
          <Route path="/session/:sessionId" element={<Session />} />
          {/* 旧版签名路由 (persistent) */}
          <Route path="/sign" element={<Sign />} />
          <Route path="/sign/:sessionId" element={<Sign />} />
          {/* Co-Sign 路由 */}
          <Route path="/cosign/create" element={<CoSignCreate />} />
          <Route path="/cosign/join" element={<CoSignJoin />} />
          <Route path="/cosign/join/:inviteCode" element={<CoSignJoin />} />
          <Route path="/cosign/session/:sessionId" element={<CoSignSession />} />
          {/* 设置 */}
          <Route path="/settings" element={<Settings />} />
          <Route path="*" element={<Navigate to="/" replace />} />
        </Routes>
      </Layout>
      <DebugConsole
        isOpen={showDebugConsole}
        onClose={() => setShowDebugConsole(false)}
      />
    </>
  );
}

export default App;
