/**
 * SSA-Pro 智能统计工作台 V11
 * 
 * 100% 还原 V11 原型图设计
 * - 左侧抽屉栏（可展开/收起）
 * - 中间对话区（居中布局）
 * - 右侧工作区（动态显示）
 * 
 * 键盘快捷键：
 * - Esc: 关闭工作区/代码模态框
 * - Ctrl+B: 切换侧边栏
 * - Ctrl+N: 新建分析
 */
import React, { useEffect, useCallback } from 'react';
import { useSSAStore } from './stores/ssaStore';
import { SSASidebar } from './components/SSASidebar';
import { SSAChatPane } from './components/SSAChatPane';
import { SSAWorkspacePane } from './components/SSAWorkspacePane';
import { SSACodeModal } from './components/SSACodeModal';
import { SSAToast } from './components/SSAToast';
import { DataProfileModal } from './components/DataProfileModal';

const SSAWorkspace: React.FC = () => {
  const {
    workspaceOpen,
    setWorkspaceOpen,
    codeModalVisible,
    setCodeModalVisible,
    sidebarExpanded,
    setSidebarExpanded,
    reset,
  } = useSSAStore();

  const handleKeyDown = useCallback((e: KeyboardEvent) => {
    if (e.key === 'Escape') {
      if (codeModalVisible) {
        setCodeModalVisible(false);
      } else if (workspaceOpen) {
        setWorkspaceOpen(false);
      }
    }

    if (e.ctrlKey || e.metaKey) {
      if (e.key === 'b' || e.key === 'B') {
        e.preventDefault();
        setSidebarExpanded(!sidebarExpanded);
      }
      if (e.key === 'n' || e.key === 'N') {
        e.preventDefault();
        reset();
      }
    }
  }, [
    codeModalVisible,
    workspaceOpen,
    sidebarExpanded,
    setCodeModalVisible,
    setWorkspaceOpen,
    setSidebarExpanded,
    reset,
  ]);

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

  return (
    <div className="ssa-v11">
      {/* Toast 容器 */}
      <SSAToast />

      {/* 左侧抽屉栏 */}
      <SSASidebar />

      {/* 主容器 */}
      <div className="ssa-v11-main">
        {/* 对话区 */}
        <SSAChatPane />

        {/* 工作区 */}
        <SSAWorkspacePane />
      </div>

      {/* 代码模态框 */}
      <SSACodeModal />
      
      {/* Phase 2A: 数据质量报告详情模态框 */}
      <DataProfileModal />
    </div>
  );
};

export default SSAWorkspace;
