"use client";
import React, { useState, useEffect, useRef } from 'react';
import Header from './components/Header';
import AI from './components/AI';
import FAQ from './components/Faq'; // Ensure the import path is correct
import Documentation from './components/Documentation'; // Ensure the import path is correct
import History from './components/History';
import Models from './components/Models';
import Credits from './components/Credits';
import Settings from './components/Settings';
import { applyIOMarketTheme, applyWhiteTheme, applyBlackTheme, applyCustomTheme } from './components/theme'
import Head from 'next/head';
import './styles/master.css';

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

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

  const handleViewChange = (view: 'AI' | 'FAQ' | 'Documentation' | 'Credits') => { // Added 'Credits' here as well
    setView(view);
    if (view !== 'AI') {
      setShowDivs(false);
    }
  };

  const [selectedTheme, setSelectedTheme] = useState<string>('');

    useEffect(() => {
      const savedTheme = localStorage.getItem('selectedTheme');
      if (savedTheme) {
        setSelectedTheme(savedTheme);
        // Apply the saved theme on initial load
        switch (savedTheme) {
          case 'IOMARKET':
            applyIOMarketTheme();
            break;
          case 'WHITE':
            applyWhiteTheme();
            break;
          case 'BLACK':
            applyBlackTheme();
            break;
          case 'CUSTOM':
            applyCustomTheme();
            break;
          default:
            applyIOMarketTheme();
            break;
        }
      }   
    }, []); // Runs only once when the component mounts          

  return (
    <>
        <Header
          toggleDivs={toggleDivs}
          showDivs={showDivs}
          onViewChange={handleViewChange}
          showHistoryModelsToggle={true}
          showToggle={view === 'AI'} // Pass the condition here
          />
      <div className="container">
        <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 />}
          {view === 'Credits' && <Credits />} {/* Now Credits will render properly */}
        </div>
      </div>
    </>
  );
};

export default LandingPage;
