import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom";
import HomeLayout from "./layouts/HomeLayout";
import SocialLayout from "./layouts/social/SocialLayout";
import ChatLayout from "./layouts/social/Chat";

import Downloader from "./pages/downloader/Downloader";
import Home from "./pages/home/home";
import DroneServisSection from "./pages/home/components/Services/droneServis";
import WebServicePage from "./pages/home/components/Services/webs";
import FilmServicePage from "./pages/home/components/Services/kinematografie";

import PrivateRoute from "./routes/PrivateRoute";
import PublicOnlyRoute from "./routes/PublicOnlyRoute";

// Pages
import PortfolioPage from "./pages/portfolio/PortfolioPage";
import ContactPage from "./pages/contact/ContactPage";
import ScrollToTop from "./components/common/ScrollToTop";
import TopBanner from "./components/common/TopBanner";

import LogoutPage from "./pages/social/account/Logout";
import LoginPage from "./pages/social/account/Login";
import RegisterPage from "./pages/social/account/Register";
import PasswordResetPage from "./pages/social/account/PasswordResetPage";
import ConfirmEmailChangePage from "./pages/social/account/ConfirmEmailChangePage";
import { RetroSoundTest } from "./pages/test/sounds";

// Social pages
import FeedPage from "./pages/social/FeedPage";
import PostPage from "./pages/social/PostPage";
import HubsPage from "./pages/social/HubsPage";
import HubPage from "./pages/social/HubPage";
import HubCreatePage from "./pages/social/hub/Create";
import HubSettingsPage from "./pages/social/hub/Settings";
import ProfilePage from "./pages/social/ProfilePage";
import UserProfilePage from "./pages/social/UserProfilePage";
import SavedPage from "./pages/social/SavedPage";
import AccountSettingsPage from "./pages/social/AccountSettingsPage";
import ChatsIndexPage from "./pages/social/chat/ChatsPage";
import ChatRoomPage from "./pages/social/chat/ChatRoomPage";

export default function App() {
  return (
    <Router>
      <ScrollToTop />
      <TopBanner />
      <Routes>
        {/* Public marketing routes */}
        <Route path="/" element={<HomeLayout />}>
          <Route index element={<Home />} />
          <Route path="portfolio" element={<PortfolioPage />} />
          <Route path="contact" element={<ContactPage />} />
          <Route path="apps/downloader" element={<Downloader />} />
          <Route path="services/drone" element={<DroneServisSection />} />
          <Route path="services/web" element={<WebServicePage />} />
          <Route path="services/film" element={<FilmServicePage />} />
          <Route path="test/sounds" element={<RetroSoundTest />} />
        </Route>

        {/* Public-only social auth */}
        <Route path="/social" element={<PublicOnlyRoute />}>
          <Route path="login" element={<LoginPage />} />
          <Route path="register" element={<RegisterPage />} />
          <Route path="password-reset" element={<PasswordResetPage />} />
        </Route>

        {/* Email change confirmation — public, verified by token */}
        <Route path="/account/confirm-email-change/:uidb64/:token" element={<ConfirmEmailChangePage />} />

        {/* Authenticated social area */}
        <Route path="/social" element={<PrivateRoute />}>
          <Route element={<SocialLayout />}>
            <Route index element={<Navigate to="/social/feed" replace />} />
            <Route path="feed" element={<FeedPage />} />
            <Route path="post/:id" element={<PostPage />} />
            <Route path="hubs" element={<HubsPage />} />
            <Route path="h/create" element={<HubCreatePage />} />
            <Route path="h/:name" element={<HubPage />} />
            <Route path="h/:name/settings" element={<HubSettingsPage />} />
            <Route path="profile" element={<ProfilePage />} />
            <Route path="profile/:username" element={<UserProfilePage />} />
            <Route path="saved" element={<SavedPage />} />
            <Route path="account/settings" element={<AccountSettingsPage />} />
            <Route path="chats" element={<ChatLayout />}>
              <Route index element={<ChatsIndexPage />} />
              <Route path=":chatId" element={<ChatRoomPage />} />
            </Route>
          </Route>
          <Route path="logout" element={<LogoutPage />} />
        </Route>

        {/* Legacy /auth redirects */}
        <Route path="/auth/login" element={<Navigate to="/social/login" replace />} />
        <Route path="/auth/register" element={<Navigate to="/social/register" replace />} />
        <Route path="/auth/logout" element={<Navigate to="/social/logout" replace />} />
      </Routes>
    </Router>
  );
}
