// imports

import { useQuery } from "@tanstack/vue-query";
import { useAppParams } from "~/composables/global/useAppParams";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";

// types

export type GetAllTicketsResponse = ApiPaginated<Omit<Ticket, "messages">>;

export type GetAllTicketsRequest = {
    sort: string | undefined;
    status: string | undefined;
    page: string | string[];
};

const useGetAllTickets = () => {
    // state

    const { $axios: axios } = useNuxtApp();

    const { sort, status, page } = useAppParams();

    // methods

    const handleGetAllTickets = async () => {
        const { data } = await axios.get<GetAllTicketsResponse>(API_ENDPOINTS.tickets.get_all, {
            params: {
                sort: sort.value ?? "created_at",
                filter: status.value,
                offset: Number(page.value) * 7 - 7,
                limit: 7,
            },
        });
        return data;
    };

    return useQuery({
        queryKey: [QUERY_KEYS.tickets, sort, status, page],
        queryFn: () => handleGetAllTickets(),
    });
};

export default useGetAllTickets;
