"use client";
import React, { useState, useEffect, useRef } from 'react';
import Header from './Header';
import AI from './AI';
import FAQ from './Faq'; // Ensure the import path is correct
import Documentation from './Documentation'; // Ensure the import path is correct
import History from './History';
import Models from './Models';
import './styles/master.css';

const LandingPage: React.FC = () => {
  const [showDivs, setShowDivs] = useState(true);
  const [view, setView] = useState<'AI' | 'FAQ' | 'Documentation'>('AI');
  const conversationRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const scrollToBottom = () => {
      const conversation = conversationRef.current;
      if (conversation) {
        conversation.scrollTop = conversation.scrollHeight;
      }
    };

    scrollToBottom();

    const observer = new MutationObserver(scrollToBottom);
    if (conversationRef.current) {
      observer.observe(conversationRef.current, {
        childList: true,
        subtree: true,
      });
    }

    return () => {
      if (conversationRef.current) {
        observer.disconnect();
      }
    };
  }, []);

  const toggleDivs = () => {
    setShowDivs(prevState => !prevState);
  };

  const handleViewChange = (view: 'AI' | 'FAQ' | 'Documentation') => {
    setView(view);
    if (view !== 'AI') {
      setShowDivs(false);
    }
  };

  return (
    <div className="container">
      <Header
        toggleDivs={toggleDivs}
        showDivs={showDivs}
        onViewChange={handleViewChange}
        showHistoryModelsToggle={true}
        showToggle={view === 'AI'} // Pass the condition here
      />
      <div className={`left-panel ${showDivs ? 'visible' : 'hidden'}`}>
        {showDivs && (
          <div className="history-models">
            <History />
            <Models />
          </div>
        )}
      </div>
      <div className={`conversation-container ${showDivs ? 'collapsed' : 'expanded'}`} ref={conversationRef}>
        {view === 'AI' && <AI />}
        {view === 'FAQ' && <FAQ />}
        {view === 'Documentation' && <Documentation />}
      </div>
    </div>
  );
};

export default LandingPage;
