'use client';

import { usePathname } from 'next/navigation';
import { Sidebar } from './Sidebar';
import { TopNavbar } from './TopNavbar';
import { FeedbackButton } from '@features/feedback/components/FeedbackButton';
import { AIAssistantButton } from '@features/ai-assistant/components/AIAssistantButton';
import { useSidebar } from '@/hooks/useSidebar';
import { getEnvBannerVariant } from '@/lib/env-banner';

export function LayoutWrapper({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const { collapsed } = useSidebar();
  const envBannerVariant = getEnvBannerVariant();
  
  // 不显示 Sidebar 和 TopNavbar 的页面
  const isLoginPage = pathname === '/login';
  // 知识库编辑器不走全屏模式
  const isKnowledgeBaseEditor = pathname.startsWith('/knowledge-base/editor');
  const isMeetingAttendance = pathname.startsWith('/meetingattendance');
  // v1.5 公共签到 / 分诊 / 大屏 都要全屏，不能有后台 Sidebar/TopNavbar
  const isSiteAttendanceCheckin =
    pathname.startsWith('/siteattendance/c/') ||
    pathname.startsWith('/siteattendance/shared/') ||
    pathname.startsWith('/siteattendance/display/');
  const isSiteAttendanceAdmin = pathname.startsWith('/siteattendance/admin/');
  // showcase 模块：静态展示页，登录可见但全屏无壳
  const isShowcase = pathname.startsWith('/showcase/');
  // FF AI Agent 主对话页：沉浸式全屏（对齐 ChatGPT/Claude），但 /agent/admin/* 仍走 shell
  const isAgentChat = pathname === '/agent' || pathname === '/agent/';
  const isFullscreenPage = !isKnowledgeBaseEditor && (
                           pathname === '/scan-inventory' ||
                           pathname.startsWith('/scan-inventory/') ||
                           isShowcase ||
                           isAgentChat ||
                           // 表单设计器全屏模式
                           pathname.includes('/design') ||
                           pathname.includes('/builder') ||
                           pathname.includes('/editor'));

  if (isLoginPage || isFullscreenPage || isMeetingAttendance || isSiteAttendanceCheckin) {
    return <>{children}</>;
  }

  return (
    <div
      className="flex h-screen page-container"
      // 顶栏高度暴露给子组件（Drawer / 浮层 fixed 定位时避让 TopNavbar）
      style={{ ['--app-top-offset' as any]: envBannerVariant ? '92px' : '64px' }}
    >
      <Sidebar />
      <TopNavbar />

      {/* Main Content */}
      <div
        className={`flex-1 ${collapsed ? 'ml-16' : 'ml-64'} ${
          envBannerVariant ? 'mt-[92px]' : 'mt-16'
        } transition-all duration-200 ${isSiteAttendanceAdmin ? 'overflow-y-visible' : 'overflow-y-auto scrollbar-custom'}`}
      >
        {children}
      </div>

      {/* 全局悬浮按钮 */}
      <AIAssistantButton />
      <FeedbackButton />
    </div>
  );
}
