import type { ComputedRef, Ref } from 'vue'
import type { RouteLocationRaw } from 'vue-router'

import type { Option as OverflowMenuOption } from '#ui/components/base/OverflowMenu.vue'
import { createContext } from '#ui/providers/create-context'

import type {
	ContentCardTableItem,
	ContentItem,
	ContentModpackCardCategory,
	ContentModpackCardProject,
	ContentModpackCardVersion,
	ContentOwner,
} from '../types'

export interface ContentModpackData {
	project: ContentModpackCardProject
	projectLink?: string | RouteLocationRaw
	version?: ContentModpackCardVersion
	versionLink?: string | RouteLocationRaw
	owner?: ContentOwner
	categories: ContentModpackCardCategory[]
	hasUpdate: boolean
	disabled?: boolean
	disabledText?: string
}

export interface UploadState {
	isUploading: boolean
	currentFileName: string | null
	currentFileProgress: number
	uploadedBytes: number
	totalBytes: number
	completedFiles: number
	totalFiles: number
}

export interface ContentManagerContext {
	// Data
	items: Ref<ContentItem[]> | ComputedRef<ContentItem[]>
	loading: Ref<boolean>
	error: Ref<Error | null>

	// Modpack
	modpack: Ref<ContentModpackData | null> | ComputedRef<ContentModpackData | null>
	isPackLocked: Ref<boolean> | ComputedRef<boolean>

	// Guards
	isBusy: Ref<boolean> | ComputedRef<boolean>
	busyMessage?: Ref<string | null> | ComputedRef<string | null>
	disableAddContent?: Ref<boolean> | ComputedRef<boolean>
	disableAddContentTooltip?: string

	// Identity & labelling
	getItemId: (item: ContentItem) => string
	contentTypeLabel: Ref<string> | ComputedRef<string>

	// Core actions
	toggleEnabled: (item: ContentItem) => Promise<void>
	deleteItem: (item: ContentItem) => Promise<void>
	refresh: () => Promise<void>
	browse: () => void
	uploadFiles: () => void

	// Bulk actions (optional — when provided, used instead of one-by-one loops)
	bulkDeleteItems?: (items: ContentItem[]) => Promise<void>
	bulkEnableItems?: (items: ContentItem[]) => Promise<void>
	bulkDisableItems?: (items: ContentItem[]) => Promise<void>

	// Update support (optional per-platform)
	hasUpdateSupport: boolean
	updateItem?: (id: string) => void
	bulkUpdateItem?: (item: ContentItem) => Promise<void>
	bulkUpdateItems?: (items: ContentItem[]) => Promise<void>

	// Modpack actions (optional)
	updateModpack?: () => void
	viewModpackContent?: () => void
	unlinkModpack?: () => void
	openSettings?: () => void

	// Per-item overflow menu (optional)
	getOverflowOptions?: (item: ContentItem) => OverflowMenuOption[]

	// Share support (optional — when undefined, share button becomes hidden entirely)
	shareItems?: (items: ContentItem[], format: 'names' | 'file-names' | 'urls' | 'markdown') => void

	// Upload progress (optional)
	uploadState?: Ref<UploadState> | ComputedRef<UploadState>

	// Show client-only environment filter pill
	showClientOnlyFilter?: boolean

	// Bulk operation guard — set by layout, checked by providers to suppress refreshes
	isBulkOperating?: Ref<boolean>

	// Deletion context (controls modal variant)
	deletionContext?: 'instance' | 'server'

	// One-time content hint (optional — shows tooltip on modpack content button)
	showContentHint?: Ref<boolean>
	dismissContentHint?: () => void

	// Table item mapping (link generation differs per platform)
	mapToTableItem: (item: ContentItem) => ContentCardTableItem
}

export const [injectContentManager, provideContentManager] = createContext<ContentManagerContext>(
	'ContentPageLayout',
	'contentManagerContext',
)
