import { create } from "zustand";

interface UIState {
	// Item delete state
	confirmDeleteItemId: number | null;

	openConfirmDelete: (itemId: number) => void;
	closeConfirmDelete: () => void;

	// Candidate delete state
	confirmDeleteCandidateId: number | null;

	openConfirmDeleteCandidate: (id: number) => void;
	closeConfirmDeleteCandidate: () => void;

	// Resolution dialog state
	resolveThreadId: number | null;
	resolveCandidateId: number | null;

	openResolveDialog: (threadId: number, candidateId: number) => void;
	closeResolveDialog: () => void;

	// Setup-related UI state
	itemPickerOpen: boolean;
	openItemPicker: () => void;
	closeItemPicker: () => void;

	confirmDeleteSetupId: number | null;
	openConfirmDeleteSetup: (id: number) => void;
	closeConfirmDeleteSetup: () => void;

	// Create thread modal
	createThreadModalOpen: boolean;
	openCreateThreadModal: () => void;
	closeCreateThreadModal: () => void;

	// External link dialog
	externalLinkUrl: string | null;
	openExternalLink: (url: string) => void;
	closeExternalLink: () => void;

	// Candidate view mode
	candidateViewMode: "list" | "grid" | "compare";
	setCandidateViewMode: (mode: "list" | "grid" | "compare") => void;

	// Setup impact preview
	selectedSetupId: number | null;
	setSelectedSetupId: (id: number | null) => void;

	// FAB menu
	fabMenuOpen: boolean;
	openFabMenu: () => void;
	closeFabMenu: () => void;

	// Catalog search overlay
	catalogSearchOpen: boolean;
	catalogSearchMode: "collection" | "thread" | null;
	openCatalogSearch: (mode: "collection" | "thread") => void;
	closeCatalogSearch: () => void;

	// Add-to-collection modal
	addToCollectionModal: {
		open: boolean;
		globalItemId: number | null;
		globalItemName: string | null;
	};
	openAddToCollection: (globalItemId: number, globalItemName: string) => void;
	closeAddToCollection: () => void;

	// Add-to-thread modal
	addToThreadModal: {
		open: boolean;
		globalItemId: number | null;
		globalItemName: string | null;
	};
	openAddToThread: (globalItemId: number, globalItemName: string) => void;
	closeAddToThread: () => void;

	// Session thread tracking
	catalogSessionThreadId: number | null;
	setCatalogSessionThreadId: (id: number | null) => void;

	// Auth prompt modal
	showAuthPrompt: boolean;
	openAuthPrompt: () => void;
	closeAuthPrompt: () => void;
}

export const useUIStore = create<UIState>((set) => ({
	// Item delete
	confirmDeleteItemId: null,

	openConfirmDelete: (itemId) => set({ confirmDeleteItemId: itemId }),
	closeConfirmDelete: () => set({ confirmDeleteItemId: null }),

	// Candidate delete
	confirmDeleteCandidateId: null,

	openConfirmDeleteCandidate: (id) => set({ confirmDeleteCandidateId: id }),
	closeConfirmDeleteCandidate: () => set({ confirmDeleteCandidateId: null }),

	// Resolution dialog
	resolveThreadId: null,
	resolveCandidateId: null,

	openResolveDialog: (threadId, candidateId) =>
		set({ resolveThreadId: threadId, resolveCandidateId: candidateId }),
	closeResolveDialog: () =>
		set({ resolveThreadId: null, resolveCandidateId: null }),

	// Setup-related UI state
	itemPickerOpen: false,
	openItemPicker: () => set({ itemPickerOpen: true }),
	closeItemPicker: () => set({ itemPickerOpen: false }),

	confirmDeleteSetupId: null,
	openConfirmDeleteSetup: (id) => set({ confirmDeleteSetupId: id }),
	closeConfirmDeleteSetup: () => set({ confirmDeleteSetupId: null }),

	// Create thread modal
	createThreadModalOpen: false,
	openCreateThreadModal: () => set({ createThreadModalOpen: true }),
	closeCreateThreadModal: () => set({ createThreadModalOpen: false }),

	// External link dialog
	externalLinkUrl: null,
	openExternalLink: (url) => set({ externalLinkUrl: url }),
	closeExternalLink: () => set({ externalLinkUrl: null }),

	// Candidate view mode
	candidateViewMode: "list",
	setCandidateViewMode: (mode) => set({ candidateViewMode: mode }),

	// Setup impact preview
	selectedSetupId: null,
	setSelectedSetupId: (id) => set({ selectedSetupId: id }),

	// FAB menu
	fabMenuOpen: false,
	openFabMenu: () => set({ fabMenuOpen: true }),
	closeFabMenu: () => set({ fabMenuOpen: false }),

	// Catalog search overlay
	catalogSearchOpen: false,
	catalogSearchMode: null,
	openCatalogSearch: (mode) =>
		set({
			catalogSearchOpen: true,
			catalogSearchMode: mode,
			fabMenuOpen: false,
		}),
	closeCatalogSearch: () =>
		set({
			catalogSearchOpen: false,
			catalogSearchMode: null,
			catalogSessionThreadId: null,
		}),

	// Add-to-collection modal
	addToCollectionModal: {
		open: false,
		globalItemId: null,
		globalItemName: null,
	},
	openAddToCollection: (globalItemId, globalItemName) =>
		set({ addToCollectionModal: { open: true, globalItemId, globalItemName } }),
	closeAddToCollection: () =>
		set({
			addToCollectionModal: {
				open: false,
				globalItemId: null,
				globalItemName: null,
			},
		}),

	// Add-to-thread modal
	addToThreadModal: { open: false, globalItemId: null, globalItemName: null },
	openAddToThread: (globalItemId, globalItemName) =>
		set({ addToThreadModal: { open: true, globalItemId, globalItemName } }),
	closeAddToThread: () =>
		set({
			addToThreadModal: {
				open: false,
				globalItemId: null,
				globalItemName: null,
			},
		}),

	// Session thread tracking
	catalogSessionThreadId: null,
	setCatalogSessionThreadId: (id) => set({ catalogSessionThreadId: id }),

	// Auth prompt modal
	showAuthPrompt: false,
	openAuthPrompt: () => set({ showAuthPrompt: true }),
	closeAuthPrompt: () => set({ showAuthPrompt: false }),
}));
