import { createContext, useContext, useState, type ReactNode } from "react";

import { useSidebar } from "@/components/ui/sidebar";

export interface ArtifactsContextType {
  artifacts: string[];
  setArtifacts: (artifacts: string[]) => void;

  selectedArtifact: string | null;

  open: boolean;
  setOpen: (open: boolean) => void;
  deselect: () => void;

  select: (artifact: string) => void;
}

const ArtifactsContext = createContext<ArtifactsContextType | undefined>(
  undefined,
);

interface ArtifactsProviderProps {
  children: ReactNode;
}

export function ArtifactsProvider({ children }: ArtifactsProviderProps) {
  const [artifacts, setArtifacts] = useState<string[]>([]);
  const [selectedArtifact, setSelectedArtifact] = useState<string | null>(null);
  const [open, setOpen] = useState(false);
  const { setOpen: setSidebarOpen } = useSidebar();

  const select = (artifact: string) => {
    setSelectedArtifact(artifact);
    setSidebarOpen(false);
  };

  const deselect = () => {
    setSelectedArtifact(null);
  };

  const value: ArtifactsContextType = {
    artifacts,
    setArtifacts,

    open,
    setOpen,

    selectedArtifact,
    select,
    deselect,
  };

  return (
    <ArtifactsContext.Provider value={value}>
      {children}
    </ArtifactsContext.Provider>
  );
}

export function useArtifacts() {
  const context = useContext(ArtifactsContext);
  if (context === undefined) {
    throw new Error("useArtifacts must be used within an ArtifactsProvider");
  }
  return context;
}
