
import { useDataStore } from "~/stores/data"

export const useEntities = (
    relation: string,
) => {

    const dataStore = useDataStore()
    const toast = useToast()
    const router = useRouter()

    const dataType = dataStore.dataTypes[relation]

    const select = async (
        select: string = "*",
        sortColumn: string | null = null,
        ascending: boolean = false,
        noArchivedFiltering: boolean = false
    ) => {

        const res = await useNuxtApp().$api(`/api/resource/${relation}`, {
            method: "GET",
            params: {
                select,
                sort: sortColumn || undefined,
                asc: ascending
            }
        })

        let data = res || []

        if (dataType && dataType.isArchivable && !noArchivedFiltering) {
            data = data.filter((i: any) => !i.archived)
        }

        return data
    }

    const selectSpecial = async (
        select: string = "*",
        sortColumn: string | null = null,
        ascending: boolean = false,
    ) => {

        const res = await useNuxtApp().$api(`/api/resource-special/${relation}`, {
            method: "GET",
            params: {
                select,
                sort: sortColumn || undefined,
                asc: ascending
            }
        })

        return res || []
    }

    const selectSingle = async (
        idToEq: string | number,
        select: string = "*",
        withInformation: boolean = false
    ) => {
        if (!idToEq) return null

        const res = await useNuxtApp().$api(withInformation ? `/api/resource/${relation}/${idToEq}/${withInformation}` : `/api/resource/${relation}/${idToEq}`, {
            method: "GET",
            params: { select }
        })

        return res
    }

    const create = async (
        payload: Record<string, any>,
        noRedirect: boolean = false
    ) => {


        const res = await useNuxtApp().$api(`/api/resource/${relation}`, {
            method: "POST",
            body: payload
        })

        toast.add({title: `${dataType.labelSingle} hinzugefügt`})
        if(dataType.redirect && !noRedirect) {
            if(dataType.isStandardEntity) {
                await router.push(dataType.redirectToList ? `/standardEntity/${relation}` : `/standardEntity/${relation}/show/${res.id}`)
            } else {
                await router.push(dataType.redirectToList ? `/${relation}` : `/${relation}/show/${res.id}`)
            }
        }
        //modal.close() TODO: Modal Close wenn in Modal


        return res
    }

    const update = async (
        id: string | number,
        payload: Record<string, any>,
        noRedirect: boolean = false
    ) => {
        const res = await useNuxtApp().$api(`/api/resource/${relation}/${id}`, {
            method: "PUT",
            body: payload
        })

        toast.add({title: `${dataType.labelSingle} geändert`})
        if(dataType.redirect && !noRedirect) {
            if(dataType.isStandardEntity) {
                await router.push(dataType.redirectToList ? `/standardEntity/${relation}` : `/standardEntity/${relation}/show/${res.id}`)
            } else {
                await router.push(dataType.redirectToList ? `/${relation}` : `/${relation}/show/${res.id}`)
            }
        }
        //modal.close() TODO: Modal Close wenn in Modal


        return res
    }

    /**
     * Soft Delete = archived = true
     */
    const archive = async (
        id: string | number
    ) => {
        const res = await useNuxtApp().$api(`/api/resource/${relation}/${id}`, {
            method: "PUT",
            body: { archived: true }
        })
        navigateTo(dataType.isStandardEntity ? `/standardEntity/${relation}` : `/${relation}`)

        return res

    }

    return {select, create, update, archive, selectSingle, selectSpecial}
}





