import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
import { pb } from "@/lib/pocketbase";
import type { Scene } from "@/lib/types";
import { useDocument } from "@/context/document/DocumentContext";
import { useQueryClient } from "@tanstack/react-query";

/**
 * Renders an editable scene form
 */
export const SceneEditForm = ({ scene }: { scene: Scene }) => {
  const { dispatch } = useDocument();
  const queryClient = useQueryClient();

  async function saveScene(text: string) {
    const updated: Scene = await pb.collection("documents").update(scene.id, {
      data: {
        ...scene.data,
        text,
      },
    });
    dispatch({ type: "setDocument", doc: updated });
  }

  return (
    <div className="">
      <AutoSaveTextarea value={scene.data.text} onSave={saveScene} />
    </div>
  );
};
