// npx vitest run src/__tests__/cloud.test.ts

import {
	organizationCloudSettingsSchema,
	organizationDefaultSettingsSchema,
	organizationFeaturesSchema,
	organizationSettingsSchema,
	userSettingsConfigSchema,
	type OrganizationCloudSettings,
	type OrganizationDefaultSettings,
	type OrganizationFeatures,
	type OrganizationSettings,
	type UserSettingsConfig,
	type WorkspaceTaskVisibility,
} from "../cloud.js"

describe("organizationFeaturesSchema", () => {
	it("should validate empty object", () => {
		const result = organizationFeaturesSchema.safeParse({})
		expect(result.success).toBe(true)
		expect(result.data).toEqual({})
	})

	it("should have correct TypeScript type", () => {
		const emptyFeatures: OrganizationFeatures = {}
		expect(emptyFeatures).toEqual({})
	})
})

describe("organizationSettingsSchema with features", () => {
	const validBaseSettings = {
		version: 1,
		defaultSettings: {},
		allowList: {
			allowAll: true,
			providers: {},
		},
	}

	it("should validate without features property", () => {
		const result = organizationSettingsSchema.safeParse(validBaseSettings)
		expect(result.success).toBe(true)
		expect(result.data?.features).toBeUndefined()
	})

	it("should validate with empty features object", () => {
		const input = {
			...validBaseSettings,
			features: {},
		}
		const result = organizationSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.features).toEqual({})
	})

	it("should have correct TypeScript type for features", () => {
		const settings: OrganizationSettings = {
			version: 1,
			defaultSettings: {},
			allowList: {
				allowAll: true,
				providers: {},
			},
			features: {},
		}
		expect(settings.features).toEqual({})

		const settingsWithoutFeatures: OrganizationSettings = {
			version: 1,
			defaultSettings: {},
			allowList: {
				allowAll: true,
				providers: {},
			},
		}
		expect(settingsWithoutFeatures.features).toBeUndefined()
	})

	it("should maintain all existing properties", () => {
		const input = {
			version: 1,
			cloudSettings: {
				recordTaskMessages: true,
				enableTaskSharing: false,
			},
			defaultSettings: {},
			allowList: {
				allowAll: false,
				providers: {
					openai: {
						allowAll: true,
						models: ["gpt-4"],
					},
				},
			},
			features: {},
			hiddenMcps: ["test-mcp"],
			hideMarketplaceMcps: true,
			mcps: [],
			providerProfiles: {},
		}
		const result = organizationSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data).toEqual(input)
	})
})

describe("organizationCloudSettingsSchema with allowPublicTaskSharing", () => {
	it("should validate without allowPublicTaskSharing property", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.allowPublicTaskSharing).toBeUndefined()
	})

	it("should validate with allowPublicTaskSharing as true", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			allowPublicTaskSharing: true,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.allowPublicTaskSharing).toBe(true)
	})

	it("should validate with allowPublicTaskSharing as false", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			allowPublicTaskSharing: false,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.allowPublicTaskSharing).toBe(false)
	})

	it("should reject non-boolean allowPublicTaskSharing", () => {
		const input = {
			allowPublicTaskSharing: "true",
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(false)
	})

	it("should have correct TypeScript type", () => {
		// Type-only test to ensure TypeScript compilation
		const settings: OrganizationCloudSettings = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			allowPublicTaskSharing: true,
		}
		expect(settings.allowPublicTaskSharing).toBe(true)

		const settingsWithoutPublicSharing: OrganizationCloudSettings = {
			recordTaskMessages: false,
		}
		expect(settingsWithoutPublicSharing.allowPublicTaskSharing).toBeUndefined()
	})

	it("should validate in organizationSettingsSchema with allowPublicTaskSharing", () => {
		const input = {
			version: 1,
			cloudSettings: {
				recordTaskMessages: true,
				enableTaskSharing: true,
				allowPublicTaskSharing: false,
			},
			defaultSettings: {},
			allowList: {
				allowAll: true,
				providers: {},
			},
		}
		const result = organizationSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.cloudSettings?.allowPublicTaskSharing).toBe(false)
	})
})

describe("organizationCloudSettingsSchema with workspaceTaskVisibility", () => {
	it("should validate without workspaceTaskVisibility property", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.workspaceTaskVisibility).toBeUndefined()
	})

	it("should validate with workspaceTaskVisibility as 'all'", () => {
		const input = {
			recordTaskMessages: true,
			workspaceTaskVisibility: "all" as WorkspaceTaskVisibility,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.workspaceTaskVisibility).toBe("all")
	})

	it("should validate with workspaceTaskVisibility as 'list-only'", () => {
		const input = {
			workspaceTaskVisibility: "list-only" as WorkspaceTaskVisibility,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.workspaceTaskVisibility).toBe("list-only")
	})

	it("should validate with workspaceTaskVisibility as 'full-lockdown'", () => {
		const input = {
			workspaceTaskVisibility: "full-lockdown" as WorkspaceTaskVisibility,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.workspaceTaskVisibility).toBe("full-lockdown")
	})

	it("should reject invalid workspaceTaskVisibility value", () => {
		const input = {
			workspaceTaskVisibility: "invalid-value",
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(false)
	})

	it("should have correct TypeScript type", () => {
		// Type-only test to ensure TypeScript compilation
		const settings: OrganizationCloudSettings = {
			recordTaskMessages: true,
			workspaceTaskVisibility: "all",
		}
		expect(settings.workspaceTaskVisibility).toBe("all")

		const settingsWithoutVisibility: OrganizationCloudSettings = {
			recordTaskMessages: false,
		}
		expect(settingsWithoutVisibility.workspaceTaskVisibility).toBeUndefined()
	})

	it("should validate in organizationSettingsSchema with workspaceTaskVisibility", () => {
		const input = {
			version: 1,
			cloudSettings: {
				recordTaskMessages: true,
				enableTaskSharing: true,
				workspaceTaskVisibility: "list-only" as WorkspaceTaskVisibility,
			},
			defaultSettings: {},
			allowList: {
				allowAll: true,
				providers: {},
			},
		}
		const result = organizationSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.cloudSettings?.workspaceTaskVisibility).toBe("list-only")
	})
})

describe("organizationCloudSettingsSchema with llmEnhancedFeaturesEnabled", () => {
	it("should validate without llmEnhancedFeaturesEnabled property", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBeUndefined()
	})

	it("should validate with llmEnhancedFeaturesEnabled as true", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			llmEnhancedFeaturesEnabled: true,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBe(true)
	})

	it("should validate with llmEnhancedFeaturesEnabled as false", () => {
		const input = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			llmEnhancedFeaturesEnabled: false,
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBe(false)
	})

	it("should reject non-boolean llmEnhancedFeaturesEnabled", () => {
		const input = {
			llmEnhancedFeaturesEnabled: "true",
		}
		const result = organizationCloudSettingsSchema.safeParse(input)
		expect(result.success).toBe(false)
	})

	it("should have correct TypeScript type", () => {
		// Type-only test to ensure TypeScript compilation
		const settings: OrganizationCloudSettings = {
			recordTaskMessages: true,
			enableTaskSharing: true,
			llmEnhancedFeaturesEnabled: true,
		}
		expect(settings.llmEnhancedFeaturesEnabled).toBe(true)

		const settingsWithoutLlmFeatures: OrganizationCloudSettings = {
			recordTaskMessages: false,
		}
		expect(settingsWithoutLlmFeatures.llmEnhancedFeaturesEnabled).toBeUndefined()
	})

	it("should validate in organizationSettingsSchema with llmEnhancedFeaturesEnabled", () => {
		const input = {
			version: 1,
			cloudSettings: {
				recordTaskMessages: true,
				enableTaskSharing: true,
				llmEnhancedFeaturesEnabled: false,
			},
			defaultSettings: {},
			allowList: {
				allowAll: true,
				providers: {},
			},
		}
		const result = organizationSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.cloudSettings?.llmEnhancedFeaturesEnabled).toBe(false)
	})
})

describe("userSettingsConfigSchema with llmEnhancedFeaturesEnabled", () => {
	it("should validate without llmEnhancedFeaturesEnabled property", () => {
		const input = {
			taskSyncEnabled: true,
		}
		const result = userSettingsConfigSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBeUndefined()
	})

	it("should validate with llmEnhancedFeaturesEnabled as true", () => {
		const input = {
			taskSyncEnabled: true,
			llmEnhancedFeaturesEnabled: true,
		}
		const result = userSettingsConfigSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBe(true)
	})

	it("should validate with llmEnhancedFeaturesEnabled as false", () => {
		const input = {
			taskSyncEnabled: true,
			llmEnhancedFeaturesEnabled: false,
		}
		const result = userSettingsConfigSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBe(false)
	})

	it("should reject non-boolean llmEnhancedFeaturesEnabled", () => {
		const input = {
			llmEnhancedFeaturesEnabled: "true",
		}
		const result = userSettingsConfigSchema.safeParse(input)
		expect(result.success).toBe(false)
	})

	it("should have correct TypeScript type", () => {
		// Type-only test to ensure TypeScript compilation
		const settings: UserSettingsConfig = {
			taskSyncEnabled: true,
			llmEnhancedFeaturesEnabled: true,
		}
		expect(settings.llmEnhancedFeaturesEnabled).toBe(true)

		const settingsWithoutLlmFeatures: UserSettingsConfig = {}
		expect(settingsWithoutLlmFeatures.llmEnhancedFeaturesEnabled).toBeUndefined()
	})

	it("should validate empty object", () => {
		const result = userSettingsConfigSchema.safeParse({})
		expect(result.success).toBe(true)
		expect(result.data).toEqual({})
	})

	it("should validate with only llmEnhancedFeaturesEnabled", () => {
		const input = {
			llmEnhancedFeaturesEnabled: true,
		}
		const result = userSettingsConfigSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.llmEnhancedFeaturesEnabled).toBe(true)
	})
})

describe("organizationDefaultSettingsSchema with disabledTools", () => {
	it("should accept disabledTools as an array of valid tool names", () => {
		const input: OrganizationDefaultSettings = {
			disabledTools: ["execute_command", "write_to_file"],
		}
		const result = organizationDefaultSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.disabledTools).toEqual(["execute_command", "write_to_file"])
	})

	it("should accept empty disabledTools array", () => {
		const input: OrganizationDefaultSettings = {
			disabledTools: [],
		}
		const result = organizationDefaultSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.disabledTools).toEqual([])
	})

	it("should accept omitted disabledTools", () => {
		const input: OrganizationDefaultSettings = {}
		const result = organizationDefaultSettingsSchema.safeParse(input)
		expect(result.success).toBe(true)
		expect(result.data?.disabledTools).toBeUndefined()
	})

	it("should reject invalid tool names in disabledTools", () => {
		const input = {
			disabledTools: ["not_a_real_tool"],
		}
		const result = organizationDefaultSettingsSchema.safeParse(input)
		expect(result.success).toBe(false)
	})
})
