import { useTheme } from "@emotion/react";
import { Box } from "@mui/material";
import DocumentComponent from "../documentComponent";
import { useEffect } from "react";
import baseUrl from "@/services/api/api.config";

export default function PreviewDocuments({ documents, fetchDocuments, fetchChunks }) {
  const theme = useTheme();

  const deleteDcoument = async (documentId) => {
    const url = `${baseUrl}/upload/delete_document/${documentId}`;

    await fetch(url, {
      method: "DELETE",
      headers: { "Content-Type": "application/json" },
    })
      .then(fetchDocuments())
      .then(fetchChunks());
  };

  useEffect(() => {}, [documents]);

  return (
    <>
      <Box
        display="flex"
        flexDirection="column"
        flexGrow={1}
        padding={2}
        mb={2}
        style={{
          border: "2px solid black",
          overflow: "hidden",
          overflowY: "scroll",
          borderRadius: "8px",
          border: "1px solid #ccc",
          backgroundColor: theme.palette.background.default,
          height: "40%",
        }}
      >
        {documents.map((document, index) => (
          <DocumentComponent
            key={index}
            documentName={document.filename}
            documentId={`Document_${document.id}`}
            documentNumber={document.id}
            deleteDocument={deleteDcoument}
          ></DocumentComponent>
        ))}
      </Box>
    </>
  );
}
