import { ref } from 'vue'
import { wordpressService, type WpPost } from '@/services/wordpress'

export function useWordpress() {
  const loading = ref(false)
  const error = ref<string | null>(null)
  const posts = ref<WpPost[]>([])
  const pageContent = ref<WpPost | null>(null)

  const fetchPosts = async (params: Record<string, any> = {}) => {
    loading.value = true
    error.value = null
    try {
      posts.value = await wordpressService.getPosts(params)
    } catch (e: any) {
      error.value = e.message || 'Error fetching posts'
      console.error(e)
    } finally {
      loading.value = false
    }
  }

  const fetchPage = async (slug: string) => {
    loading.value = true
    error.value = null
    try {
      pageContent.value = await wordpressService.getPageBySlug(slug)
    } catch (e: any) {
      error.value = e.message || 'Error fetching page'
      console.error(e)
    } finally {
      loading.value = false
    }
  }

  return {
    loading,
    error,
    posts,
    pageContent,
    fetchPosts,
    fetchPage
  }
}
