import { create } from "zustand";
import { EventDeliveryType } from "@/constants/enums";
import type { Pagination, Tag } from "@/types/utils/types";
import type { UtilityStoreType } from "@/types/zustand/utility";

export const useUtilityStore = create<UtilityStoreType>((set, get) => ({
  awaitingBotResponse: false,
  setAwaitingBotResponse: (awaitingBotResponse: boolean) =>
    set({ awaitingBotResponse }),
  clientId: "",
  setClientId: (clientId: string) => set({ clientId }),
  chatValueStore: "",
  setChatValueStore: (value: string) => set({ chatValueStore: value }),
  selectedItems: [] as string[],
  setSelectedItems: (itemId: string) => {
    if (get().selectedItems.includes(itemId)) {
      set({
        selectedItems: get().selectedItems.filter((item) => item !== itemId),
      });
    } else {
      set({ selectedItems: get().selectedItems.concat(itemId) });
    }
  },
  healthCheckTimeout: null,
  setHealthCheckTimeout: (timeout: string | null) =>
    set({ healthCheckTimeout: timeout }),
  playgroundScrollBehaves: "instant",
  setPlaygroundScrollBehaves: (behaves: ScrollBehavior) =>
    set({ playgroundScrollBehaves: behaves }),
  maxFileSizeUpload: 100 * 1024 * 1024, // 100MB in bytes
  setMaxFileSizeUpload: (maxFileSizeUpload: number) =>
    set({ maxFileSizeUpload: maxFileSizeUpload * 1024 * 1024 }),
  serializationMaxItemsLength: 100,
  setSerializationMaxItemsLength: (serializationMaxItemsLength: number) =>
    set({ serializationMaxItemsLength }),
  flowsPagination: {
    page: 1,
    size: 10,
  },
  setFlowsPagination: (flowsPagination: Pagination) => set({ flowsPagination }),
  tags: [],
  setTags: (tags: Tag[]) => set({ tags }),
  featureFlags: {},
  setFeatureFlags: (featureFlags: Record<string, unknown>) =>
    set({ featureFlags }),
  webhookPollingInterval: 0, // Disabled: SSE provides real-time updates
  setWebhookPollingInterval: (webhookPollingInterval: number) =>
    set({ webhookPollingInterval }),
  currentSessionId: "",
  setCurrentSessionId: (sessionId: string) =>
    set({ currentSessionId: sessionId }),
  eventDelivery: EventDeliveryType.STREAMING,
  setEventDelivery: (eventDelivery: EventDeliveryType) =>
    set({ eventDelivery }),
  webhookAuthEnable: true,
  setWebhookAuthEnable: (webhookAuthEnable: boolean) =>
    set({ webhookAuthEnable }),
  defaultFolderName: "Starter Project",
  setDefaultFolderName: (defaultFolderName: string) =>
    set({ defaultFolderName }),
  hideGettingStartedProgress: false,
  setHideGettingStartedProgress: (hideGettingStartedProgress: boolean) =>
    set({ hideGettingStartedProgress }),
  allowCustomComponents: true,
  setAllowCustomComponents: (allowCustomComponents: boolean) =>
    set({ allowCustomComponents }),
  mcpBaseUrl: "",
  setMcpBaseUrl: (mcpBaseUrl: string) => set({ mcpBaseUrl }),
  // Default ``false`` so a misconfigured store (no ``/config`` reply yet)
  // matches the backend default of "reload disabled".  The /config query
  // overwrites this on first load.
  enableExtensionReload: false,
  setEnableExtensionReload: (enableExtensionReload: boolean) =>
    set({ enableExtensionReload }),
  // Embedded mode flags
  embeddedMode: false,
  setEmbeddedMode: (embeddedMode: boolean) => set({ embeddedMode }),
  hideLogoutButton: false,
  setHideLogoutButton: (hideLogoutButton: boolean) => set({ hideLogoutButton }),
  hideNewProjectButton: false,
  setHideNewProjectButton: (hideNewProjectButton: boolean) =>
    set({ hideNewProjectButton }),
  hideNewFlowButton: false,
  setHideNewFlowButton: (hideNewFlowButton: boolean) =>
    set({ hideNewFlowButton }),
  hideStarterProjects: false,
  setHideStarterProjects: (hideStarterProjects: boolean) =>
    set({ hideStarterProjects }),
  mcpServersLocked: false,
  setMcpServersLocked: (mcpServersLocked: boolean) => set({ mcpServersLocked }),
  customComponentAdminOnly: false,
  setCustomComponentAdminOnly: (customComponentAdminOnly: boolean) =>
    set({ customComponentAdminOnly }),
}));
