// ...existing code...
"use client";

import { useEffect, useState } from "react";
import ModelViewer from "./ModelViewer";
import Box from "@mui/material/Box";
import { CircularProgress } from "@mui/material";

type Hotspots = {
  id: string;
  color?: string;
  title: string;
  normal: string;
  position: string;
  description?: string | null;
};

export type Models = {
  id: number;
  title: string;
  description: string;
  speachtext?: string;
  image: string;
  modelSrc: string;
  category: string;
  program?: string;
  size?: string; // e.g. "84.58 MB"
  date?: string; // e.g. "Jan 28, 2026"
  stackCount?: number;
  tag?: string | null;
  createdAt?: string; // ISO date string
  updatedAt?: string; // ISO date string
  hotspots: Hotspots[];
};



type PendingHit = { position: string; normal: string } | null;

// const LABELS: Hotspots[] = [
//   {
//     id: "1769159810016",
//     color: "#4F46E5",
//     title: "arota",
//     normal: "-0.2607027611678235 0.20385461235882174 0.9436510834728626",
//     position:
//       "0.0023977800979993772 0.15188296254679487 -0.0037125437117008697",
//   },
//   {
//     id: "1769159821295",
//     color: "#4F46E5",
//     title: "interior venacava",
//     normal: "-0.2758680882400917 -0.15852132312898456 0.948033642865164",
//     position:
//       "-0.0010786242255147985 0.15065669335743007 0.00017800339332695953",
//   },
//   {
//     id: "1769159844090",
//     color: "#4F46E5",
//     title: "uriniary bladder",
//     normal: "0.07786270613276476 0.9040622944740692 0.4202484583005205",
//     position: "0.0005894118441141927 0.11577229594552374 0.00141229974937631",
//   },
//   {
//     id: "1769159877962",
//     color: "#4F46E5",
//     title: "urethera",
//     normal: "-0.09308744514914646 -0.27022860348483857 0.9582855677793749",
//     position:
//       "-0.00003160502177540413 0.11116913204508913 -0.0014521368130784875",
//   },
//   {
//     id: "1769159902675",
//     color: "#4F46E5",
//     title: "left kidney",
//     normal: "0.09959891801053512 0.4871400077765798 0.8676258804084654",
//     position: "0.008118954210167348 0.14322039248683746 -0.002361464017771057",
//   },
//   {
//     id: "1769159912337",
//     color: "#4F46E5",
//     title: "right kideny",
//     normal: "0.09549487072136117 0.24684320945429242 0.964338716225899",
//     position:
//       "-0.007516617592739063 0.14105889723183535 -0.0013936913564640916",
//   },
//   {
//     id: "1769159961762",
//     color: "#4F46E5",
//     title: "adneral glands",
//     normal: "0.08616079179359609 0.8204358953127775 0.565209040656483",
//     position:
//       "-0.005594690633534702 0.14597656349535176 -0.002623886875688445",
//   },
// ];

 const ThreeModelView: React.FC = () => {
  const [labels, setLabels] = useState<Hotspots[]>([]);
  const [adminMode, setAdminMode] = useState<boolean>(false);
  // const [draftTitle, setDraftTitle] = useState<string>("new label");
  // const [draftColor, setDraftColor] = useState<string>("#4F46E5");
  const [pendingHit, setPendingHit] = useState<PendingHit>(null);
  const [activeLabelId, setActiveLabelId] = useState<string | null>(null);
  const [loading, setLoading] = useState(true);
  const [models, setModels] = useState<Models[]>([]);
  const [page, setPage] = useState(1);
  const [hasMore, setHasMore] = useState(true);
  const [openModel, setOpenModel] = useState(false);


  const fetchModels = async () => {
    setLoading(true);
    const res = await fetch(`/api/models?page=${page}&limit=5`);
    const data = await res.json();
    console.log("Fetched models:", data);
    setModels((prev) => [...prev, ...data.data]);
    setHasMore(data.hasMore);
    setLoading(false);
  };
  const loadMore = () => {
    if (hasMore) setPage((prev) => prev + 1);
  };


  useEffect(() => {
    (async () => {
      await fetchModels();
    })();
  }, [page]);



  // const handlePickPosition = (hit: { position: string; normal: string }) => {
  //   setPendingHit({ position: hit.position, normal: hit.normal });
  // };

  // const handleSaveLabel = () => {
  //   if (!pendingHit) return;
  //   const title = draftTitle.trim() || "untitled";
  //   setLabels((prev) => [
  //     {
  //       id: `${Date.now()}`,
  //       color: draftColor,
  //       title,
  //       normal: pendingHit.normal,
  //       position: pendingHit.position,
  //     },
  //     ...prev,
  //   ]);
  //   setPendingHit(null);
  // };

  // const removeLabel = (id: string) => {
  //   setLabels((prev) => prev.filter((label) => label.id !== id));
  //   setActiveLabelId((prev) => (prev === id ? null : prev));
  // };
  // Show loading state
  if (loading) {
    return (
      <Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '400px' }}>
        <CircularProgress />
      </Box>
    );
  }
  return (
    // <div className="page">
      <section className="demo">
        <div className="viewer-panel">
          <ModelViewer
            modelUrl={"/assets/kidney.glb"}
            labels={labels}
            enableAdd={adminMode}
            onAddLabel={()=>{}}
            previewPosition={pendingHit?.position}
            activeLabelId={activeLabelId}
          />
        </div>
        {/* ...existing code... */}
      </section>
    // </div>
  );
}

export default ThreeModelView;