"use client";

import { useEffect } from "react";
import { useAppStore, useAuthStore } from "@/lib/store";
import { Navbar } from "./navbar";
import { Footer } from "./footer";
import { MobileNav } from "./mobile-nav";
import { HomeView } from "./views/home-view";
import { MoviesView } from "./views/movies-view";
import { SeriesView } from "./views/series-view";
import { DetailView } from "./views/detail-view";
import { WatchView } from "./views/watch-view";
import { LiveView } from "./views/live-view";
import { MusicView } from "./views/music-view";
import { SearchView } from "./views/search-view";
import { SubscriptionView } from "./views/subscription-view";
import { ProfileView } from "./views/profile-view";
import { AdminView } from "./views/admin-view";
import { AiCenterView } from "./views/ai-center-view";
import { SupportView } from "./views/support-view";

export function AppShell() {
  const view = useAppStore((s) => s.view);
  const setUser = useAuthStore((s) => s.setUser);

  // Restore session from localStorage (demo only)
  useEffect(() => {
    try {
      const raw = typeof window !== "undefined" ? localStorage.getItem("peepbox_user") : null;
      if (raw) setUser(JSON.parse(raw));
    } catch {}
  }, [setUser]);

  // Scroll to top on view change
  useEffect(() => {
    window.scrollTo({ top: 0, behavior: "smooth" });
  }, [view]);

  return (
    <div className="min-h-screen flex flex-col">
      <Navbar />
      <main className="flex-1 pt-16 pb-16 md:pb-0">
        {renderView(view)}
      </main>
      <Footer />
      <MobileNav />
    </div>
  );
}

function renderView(view: string) {
  switch (view) {
    case "home": return <HomeView />;
    case "movies": return <MoviesView />;
    case "series": return <SeriesView />;
    case "detail": return <DetailView />;
    case "watch": return <WatchView />;
    case "live": return <LiveView />;
    case "music": return <MusicView />;
    case "search": return <SearchView />;
    case "subscription": return <SubscriptionView />;
    case "profile": return <ProfileView />;
    case "favorites": return <ProfileView initialTab="favorites" />;
    case "history": return <ProfileView initialTab="history" />;
    case "admin": return <AdminView />;
    case "ai-center": return <AiCenterView />;
    case "support": return <SupportView />;
    default: return <HomeView />;
  }
}
