import React from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { AuthProvider, useAuth } from './src/contexts/AuthContext';
import { LanguageProvider } from './src/contexts/LanguageContext';
import { ThemeProvider } from './src/contexts/ThemeContext';
import Navbar from './components/Navbar';
import CastADApp from './src/pages/CastADApp';
import LoginPage from './src/pages/LoginPage';
import RegisterPage from './src/pages/RegisterPage';
import ForgotPasswordPage from './src/pages/ForgotPasswordPage';
import ResetPasswordPage from './src/pages/ResetPasswordPage';
import VerifyEmailPage from './src/pages/VerifyEmailPage';
import OAuthCallbackPage from './src/pages/OAuthCallbackPage';
import AdminDashboard from './src/pages/AdminDashboard';
import LandingPage from './src/pages/LandingPage';
import BrandPage from './src/pages/BrandPage';
import CreditsPage from './src/pages/CreditsPage';
import './src/styles/globals.css';
import './src/styles/text-effects.css';
import './src/styles/animations.css';

// 홈 라우트: 로그인 여부에 따라 랜딩 또는 SaaS 앱 표시
const HomeRoute: React.FC = () => {
  const { user } = useAuth();
  return user ? <CastADApp /> : <LandingPage />;
};

// SaaS 앱 보호 라우트: 로그인 필요
const ProtectedAppRoute: React.FC = () => {
  const { user } = useAuth();
  return user ? <CastADApp /> : <Navigate to="/login" />;
};

// 랜딩 페이지용 라우트 (Navbar 포함)
const PublicRoutes: React.FC = () => {
  return (
    <>
      <Navbar />
      <div className="pt-16 min-h-screen bg-background text-foreground font-sans">
        <Routes>
          <Route path="/login" element={<LoginPage />} />
          <Route path="/register" element={<RegisterPage />} />
          <Route path="/forgot-password" element={<ForgotPasswordPage />} />
          <Route path="/reset-password" element={<ResetPasswordPage />} />
          <Route path="/verify-email" element={<VerifyEmailPage />} />
          <Route path="/oauth/callback" element={<OAuthCallbackPage />} />
          <Route path="/admin" element={<AdminDashboard />} />
          <Route path="/brand" element={<BrandPage />} />
          <Route path="/" element={<LandingPage />} />
          <Route path="/app/*" element={<Navigate to="/login" />} />
          <Route path="*" element={<Navigate to="/" />} />
        </Routes>
      </div>
    </>
  );
};

const AppRoutes: React.FC = () => {
  const { user } = useAuth();

  // 로그인된 사용자는 SaaS 앱으로 (Navbar 없음, Sidebar 사용)
  if (user) {
    return (
      <Routes>
        <Route path="/login" element={<Navigate to="/app" />} />
        <Route path="/register" element={<Navigate to="/app" />} />
        <Route path="/forgot-password" element={<Navigate to="/app" />} />
        <Route path="/reset-password" element={<ResetPasswordPage />} />
        <Route path="/verify-email" element={<VerifyEmailPage />} />
        <Route path="/oauth/callback" element={<OAuthCallbackPage />} />
        <Route path="/admin" element={<AdminDashboard />} />
        <Route path="/brand" element={<BrandPage />} />
        <Route path="/credits" element={<CreditsPage />} />
        <Route path="/app/*" element={<CastADApp />} />
        <Route path="/" element={<Navigate to="/app" />} />
        <Route path="*" element={<Navigate to="/app" />} />
      </Routes>
    );
  }

  // 로그인되지 않은 사용자는 공개 페이지로
  return <PublicRoutes />;
};

const App: React.FC = () => {
  return (
    <AuthProvider>
      <LanguageProvider>
        <ThemeProvider>
          <BrowserRouter>
            <AppRoutes />
          </BrowserRouter>
        </ThemeProvider>
      </LanguageProvider>
    </AuthProvider>
  );
};

export default App;
