import { useMutation } from '@tanstack/react-query';
import { toast } from 'sonner';

import { LocalPageNode, LocalSpaceNode } from '@colanode/client/types';
import { generateId, IdType } from '@colanode/core';
import {
  SpaceForm,
  SpaceFormValues,
} from '@colanode/ui/components/spaces/space-form';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from '@colanode/ui/components/ui/dialog';
import { useWorkspace } from '@colanode/ui/contexts/workspace';

interface SpaceCreateDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
}

export const SpaceCreateDialog = ({
  open,
  onOpenChange,
}: SpaceCreateDialogProps) => {
  const workspace = useWorkspace();

  const { mutate } = useMutation({
    mutationFn: async (values: SpaceFormValues) => {
      const nodes = workspace.collections.nodes;

      const spaceId = generateId(IdType.Space);
      const space: LocalSpaceNode = {
        id: spaceId,
        parentId: null,
        type: 'space',
        name: values.name,
        description: values.description,
        avatar: values.avatar,
        collaborators: {
          [workspace.userId]: 'admin',
        },
        visibility: 'private',
        rootId: spaceId,
        createdAt: new Date().toISOString(),
        createdBy: workspace.userId,
        updatedAt: null,
        updatedBy: null,
        localRevision: '0',
        serverRevision: '0',
      };

      const pageId = generateId(IdType.Page);
      const page: LocalPageNode = {
        id: pageId,
        type: 'page',
        name: 'Home',
        parentId: spaceId,
        rootId: spaceId,
        createdAt: new Date().toISOString(),
        createdBy: workspace.userId,
        updatedAt: null,
        updatedBy: null,
        localRevision: '0',
        serverRevision: '0',
      };

      nodes.insert([space, page]);
      return space;
    },
    onSuccess: () => {
      onOpenChange(false);
    },
    onError: (error) => {
      toast.error(error.message);
    },
  });

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="w-xl max-w-full">
        <DialogHeader>
          <DialogTitle>Create space</DialogTitle>
          <DialogDescription>
            Create a new space to collaborate with your peers
          </DialogDescription>
        </DialogHeader>
        <SpaceForm
          onSubmit={(values) => mutate(values)}
          submitText="Create"
          onCancel={() => onOpenChange(false)}
        />
      </DialogContent>
    </Dialog>
  );
};
