import { useEffect } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { BrowserView, MobileView } from 'react-device-detect';

// 공통 컴포넌트
import ScrollToTop from './components/ScrollToTop';

// PC 페이지
import PCHome from './pages/pc/public/Home';
import PCMembers from './pages/pc/public/Members';
import PCAlbum from './pages/pc/public/Album';
import PCAlbumDetail from './pages/pc/public/AlbumDetail';
import PCAlbumGallery from './pages/pc/public/AlbumGallery';
import PCTrackDetail from './pages/pc/public/TrackDetail';
import PCSchedule from './pages/pc/public/Schedule';
import PCScheduleDetail from './pages/pc/public/ScheduleDetail';
import PCNotFound from './pages/pc/public/NotFound';

// 모바일 페이지
import MobileHome from './pages/mobile/public/Home';
import MobileMembers from './pages/mobile/public/Members';
import MobileAlbum from './pages/mobile/public/Album';
import MobileAlbumDetail from './pages/mobile/public/AlbumDetail';
import MobileAlbumGallery from './pages/mobile/public/AlbumGallery';
import MobileTrackDetail from './pages/mobile/public/TrackDetail';
import MobileSchedule from './pages/mobile/public/Schedule';
import MobileNotFound from './pages/mobile/public/NotFound';

// 관리자 페이지
import AdminLogin from './pages/pc/admin/AdminLogin';
import AdminDashboard from './pages/pc/admin/AdminDashboard';
import AdminMembers from './pages/pc/admin/AdminMembers';
import AdminMemberEdit from './pages/pc/admin/AdminMemberEdit';
import AdminAlbums from './pages/pc/admin/AdminAlbums';
import AdminAlbumForm from './pages/pc/admin/AdminAlbumForm';
import AdminAlbumPhotos from './pages/pc/admin/AdminAlbumPhotos';
import AdminSchedule from './pages/pc/admin/AdminSchedule';
import AdminScheduleForm from './pages/pc/admin/AdminScheduleForm';
import AdminScheduleCategory from './pages/pc/admin/AdminScheduleCategory';
import AdminScheduleBots from './pages/pc/admin/AdminScheduleBots';

// 레이아웃
import PCLayout from './components/pc/Layout';
import MobileLayout from './components/mobile/Layout';

// PC 환경에서 body에 클래스 추가하는 래퍼
function PCWrapper({ children }) {
    useEffect(() => {
        document.body.classList.add('is-pc');
        return () => document.body.classList.remove('is-pc');
    }, []);
    return children;
}

function App() {
    return (
        <BrowserRouter future={{ v7_startTransition: true, v7_relativeSplatPath: true }}>
            <ScrollToTop />
            <BrowserView>
                <PCWrapper>
                <Routes>
                    {/* 관리자 페이지 (레이아웃 없음) */}
                    <Route path="/admin" element={<AdminLogin />} />
                    <Route path="/admin/dashboard" element={<AdminDashboard />} />
                    <Route path="/admin/members" element={<AdminMembers />} />
                    <Route path="/admin/members/:name/edit" element={<AdminMemberEdit />} />
                    <Route path="/admin/albums" element={<AdminAlbums />} />
                    <Route path="/admin/albums/new" element={<AdminAlbumForm />} />
                    <Route path="/admin/albums/:id/edit" element={<AdminAlbumForm />} />
                    <Route path="/admin/albums/:albumId/photos" element={<AdminAlbumPhotos />} />
                    <Route path="/admin/schedule" element={<AdminSchedule />} />
                    <Route path="/admin/schedule/new" element={<AdminScheduleForm />} />
                    <Route path="/admin/schedule/:id/edit" element={<AdminScheduleForm />} />
                    <Route path="/admin/schedule/categories" element={<AdminScheduleCategory />} />
                    <Route path="/admin/schedule/bots" element={<AdminScheduleBots />} />

                    {/* 일반 페이지 (레이아웃 포함) */}
                    <Route path="/*" element={
                        <PCLayout>
                            <Routes>
                                <Route path="/" element={<PCHome />} />
                                <Route path="/members" element={<PCMembers />} />
                                <Route path="/album" element={<PCAlbum />} />
                                <Route path="/album/:name" element={<PCAlbumDetail />} />
                                <Route path="/album/:name/gallery" element={<PCAlbumGallery />} />
                                <Route path="/album/:name/track/:trackTitle" element={<PCTrackDetail />} />
                                <Route path="/schedule" element={<PCSchedule />} />
                                <Route path="/schedule/:id" element={<PCScheduleDetail />} />
                                <Route path="*" element={<PCNotFound />} />
                            </Routes>
                        </PCLayout>
                    } />
                </Routes>
                </PCWrapper>
            </BrowserView>
            <MobileView>
                <Routes>
                    <Route path="/" element={<MobileLayout><MobileHome /></MobileLayout>} />
                    <Route path="/members" element={<MobileLayout pageTitle="멤버" noShadow><MobileMembers /></MobileLayout>} />
                    <Route path="/album" element={<MobileLayout pageTitle="앨범"><MobileAlbum /></MobileLayout>} />
                    <Route path="/album/:name" element={<MobileLayout pageTitle="앨범"><MobileAlbumDetail /></MobileLayout>} />
                    <Route path="/album/:name/gallery" element={<MobileLayout pageTitle="앨범"><MobileAlbumGallery /></MobileLayout>} />
                    <Route path="/album/:name/track/:trackTitle" element={<MobileLayout pageTitle="앨범"><MobileTrackDetail /></MobileLayout>} />
                    <Route path="/schedule" element={<MobileLayout useCustomLayout><MobileSchedule /></MobileLayout>} />
                    <Route path="*" element={<MobileNotFound />} />
                </Routes>
            </MobileView>
        </BrowserRouter>
    );
}

export default App;

