// composables/useHelpdeskApi.ts
import { ref } from 'vue'
import { useNuxtApp } from '#app'

export function useHelpdeskApi() {
    const { $api } = useNuxtApp()
    const loading = ref(false)
    const error = ref<string | null>(null)

    const base = '/api/helpdesk'

    // 🔹 Konversationen abrufen
    async function getConversations(status?: string) {
        loading.value = true
        error.value = null
        try {
            const query = status ? `?status=${status}` : ''
            const data = await $api(`${base}/conversations${query}`)
            return data
        } catch (err: any) {
            error.value = err.message || 'Fehler beim Laden der Konversationen'
            return []
        } finally {
            loading.value = false
        }
    }

    // 🔹 Einzelne Konversation
    async function getConversation(id: string) {
        try {
            return await $api(`${base}/conversations/${id}`)
        } catch (err: any) {
            error.value = err.message
            return null
        }
    }

    // 🔹 Nachrichten einer Konversation
    async function getMessages(conversationId: string) {
        try {
            return await $api(`${base}/conversations/${conversationId}/messages`)
        } catch (err: any) {
            error.value = err.message
            return []
        }
    }

    // 🔹 Neue Nachricht senden
    async function sendMessage(conversationId: string, text: string) {
        try {
            return await $api(`${base}/conversations/${conversationId}/messages`, {
                method: 'POST',
                body: { text },
            })
        } catch (err: any) {
            error.value = err.message
            throw err
        }
    }

    async function replyMessage(conversationId: string, text: string) {
        try {
            return await $api(`${base}/conversations/${conversationId}/reply`, {
                method: 'POST',
                body: { text },
            })
        } catch (err: any) {
            error.value = err.message
            throw err
        }
    }

    // 🔹 Neuen Kontakt (manuell) anlegen
    async function createContact(payload: { email?: string; phone?: string; display_name?: string }) {
        try {
            return await $api(`${base}/contacts`, {
                method: 'POST',
                body: payload,
            })
        } catch (err: any) {
            error.value = err.message
            throw err
        }
    }

    // 🔹 Konversation-Status ändern
    async function updateConversationStatus(conversationId: string, status: string) {
        try {
            return await $api(`${base}/conversations/${conversationId}/status`, {
                method: 'PATCH',
                body: { status },
            })
        } catch (err: any) {
            error.value = err.message
            throw err
        }
    }

    return {
        loading,
        error,
        getConversations,
        getConversation,
        getMessages,
        sendMessage,
        createContact,
        updateConversationStatus,
        replyMessage,
    }
}
