import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
import { api } from "@/lib/api"
import Swal from "sweetalert2"

const Toast = Swal.mixin({
    toast: true,
    position: 'top-end',
    showConfirmButton: false,
    timer: 3000,
    timerProgressBar: true,
})

export interface MainMenu {
    id: string
    home: string
    about: string
    services: string
    resume: string
    portfolio: string
    contact: string
    is_active: boolean
    created_at: string
    updated_at: string
}

export const useMainMenus = () => {
    return useQuery<MainMenu[]>({
        queryKey: ["main-menus"],
        queryFn: async () => {
            const response = await api.get("/v1/admin/main-menu")
            return response.data
        },
    })
}

export const useMainMenu = (id: string) => {
    return useQuery<MainMenu>({
        queryKey: ["main-menus", id],
        queryFn: async () => {
            const response = await api.get(`/v1/admin/main-menu/${id}`)
            return response.data
        },
        enabled: !!id,
    })
}

export const useCreateMainMenu = () => {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: async (data: Partial<MainMenu>) => {
            const response = await api.post("/v1/admin/main-menu", data)
            return response.data
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["main-menus"] })
            Toast.fire({
                icon: 'success',
                title: 'Menü başarıyla oluşturuldu'
            })
        },
        onError: (error: any) => {
            Toast.fire({
                icon: 'error',
                title: error.response?.data?.error || "Menü oluşturulurken bir hata oluştu"
            })
        },
    })
}

export const useUpdateMainMenu = () => {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: async ({ id, data }: { id: string; data: Partial<MainMenu> }) => {
            const response = await api.put(`/v1/admin/main-menu/${id}`, data)
            return response.data
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["main-menus"] })
            Toast.fire({
                icon: 'success',
                title: 'Menü başarıyla güncellendi'
            })
        },
        onError: (error: any) => {
            Toast.fire({
                icon: 'error',
                title: error.response?.data?.error || "Menü güncellenirken bir hata oluştu"
            })
        },
    })
}

export const useDeleteMainMenu = () => {
    const queryClient = useQueryClient()

    return useMutation({
        mutationFn: async (id: string) => {
            const response = await api.delete(`/v1/admin/main-menu/${id}`)
            return response.data
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ["main-menus"] })
            Toast.fire({
                icon: 'success',
                title: 'Menü başarıyla silindi'
            })
        },
        onError: (error: any) => {
            Toast.fire({
                icon: 'error',
                title: error.response?.data?.error || "Menü silinirken bir hata oluştu"
            })
        },
    })
}
