"use client";

import type { TelemetrySettings, UsageLimits } from "@giselles-ai/giselle";
import type { ReactNode } from "react";
import {
	DataStoreProvider,
	type DataStoreProviderProps,
} from "../data-store/context";
import {
	FeatureFlagContext,
	type FeatureFlagContextValue,
} from "../feature-flags";
import { ZustandBridgeGenerationProvider } from "../generations";
import {
	IntegrationProvider,
	type IntegrationProviderProps,
} from "../integrations";
import { TelemetryProvider } from "../telemetry";
import { TriggerContext, type TriggerContextValue } from "../trigger";
import { UsageLimitsProvider } from "../usage-limits";
import {
	VectorStoreContext,
	type VectorStoreContextValue,
} from "../vector-store";

export function WorkspaceProvider({
	children,
	integration,
	usageLimits,
	telemetry,
	featureFlag,
	vectorStore,
	dataStore,
	trigger,
	generationTimeout,
}: {
	children: ReactNode;
	integration?: IntegrationProviderProps;
	usageLimits?: UsageLimits;
	telemetry?: TelemetrySettings;
	featureFlag?: FeatureFlagContextValue;
	vectorStore?: VectorStoreContextValue;
	dataStore?: DataStoreProviderProps;
	trigger?: TriggerContextValue;
	generationTimeout?: number;
}) {
	return (
		<FeatureFlagContext
			value={{
				webSearchAction: featureFlag?.webSearchAction ?? false,
				layoutV3: featureFlag?.layoutV3 ?? false,
				stage: featureFlag?.stage ?? false,
				aiGateway: featureFlag?.aiGateway ?? false,
				aiGatewayUnsupportedModels:
					featureFlag?.aiGatewayUnsupportedModels ?? false,
				generateContentNode: featureFlag?.generateContentNode ?? false,
				privatePreviewTools: featureFlag?.privatePreviewTools ?? false,
			}}
		>
			<TelemetryProvider settings={telemetry}>
				<TriggerContext value={trigger ?? {}}>
					<UsageLimitsProvider limits={usageLimits}>
						<IntegrationProvider {...integration}>
							<VectorStoreContext value={vectorStore}>
								<DataStoreProvider {...dataStore}>
									<ZustandBridgeGenerationProvider timeout={generationTimeout}>
										{children}
									</ZustandBridgeGenerationProvider>
								</DataStoreProvider>
							</VectorStoreContext>
						</IntegrationProvider>
					</UsageLimitsProvider>
				</TriggerContext>
			</TelemetryProvider>
		</FeatureFlagContext>
	);
}
