'use client'

import { createContext, useContext, ReactNode } from 'react'
import { useFilterStore } from '@/store/filter-store'
import { GlobalFilterSettings } from '@/types/filter.types'

/**
 * GlobalFilterContext - Zustand store 래퍼
 * 기존 코드 호환성을 위해 Context API 인터페이스 유지
 */
interface GlobalFilterContextType {
  filters: GlobalFilterSettings
  updateFilters: (newFilters: Partial<GlobalFilterSettings>) => void
  resetFilters: () => void
  isLoading: boolean
}

const GlobalFilterContext = createContext<GlobalFilterContextType | undefined>(undefined)

export function GlobalFilterProvider({ children }: { children: ReactNode }) {
  const { filters, updateFilters, resetFilters, isLoading } = useFilterStore()

  return (
    <GlobalFilterContext.Provider value={{ filters, updateFilters, resetFilters, isLoading }}>
      {children}
    </GlobalFilterContext.Provider>
  )
}

export function useGlobalFilter() {
  const context = useContext(GlobalFilterContext)
  if (context === undefined) {
    throw new Error('useGlobalFilter must be used within a GlobalFilterProvider')
  }
  return context
}
