"use server";

import { fetchDataWithToken } from "../api-fetcher";
import { baseUrlApplication } from "../basics";
import { PaginationParams } from "../schemas/list";
import type { PaginatedApiResponse } from "@/app/api/utils/types";

const servicesListEndpoint = `${baseUrlApplication}/service/list`;
const servicesToEventsEndpoint = `${baseUrlApplication}/service/to/events`;

async function listServices(payload: PaginationParams): Promise<PaginatedApiResponse<any>> {
    try {
        const response = await fetchDataWithToken(
            servicesListEndpoint,
            {
                page: payload.page,
                size: payload.size,
                order_field: payload.orderField,
                order_type: payload.orderType,
                query: payload.query,
            },
            "POST",
            false
        );

        if (response?.status === 200 || response?.status === 202) {
            const responseData = response.data as PaginatedApiResponse<any>;
            return {
                data: responseData.data || [],
                pagination: {
                    page: responseData.pagination?.page || 1,
                    size: responseData.pagination?.size || 10,
                    totalCount: responseData.pagination?.totalCount || 0,
                    totalItems: responseData.pagination?.totalItems || 0,
                    totalPages: responseData.pagination?.totalPages || 0,
                    pageCount: responseData.pagination?.pageCount || 0,
                    orderField: responseData.pagination?.orderField || ['name'],
                    orderType: responseData.pagination?.orderType || ['asc'],
                    query: responseData.pagination?.query || {},
                    next: responseData.pagination?.next || false,
                    back: responseData.pagination?.back || false
                }
            };
        }
        return {
            data: [],
            pagination: {
                page: 1,
                size: 10,
                totalCount: 0,
                totalItems: 0,
                totalPages: 0,
                pageCount: 0,
                orderField: ['name'],
                orderType: ['asc'],
                query: {},
                next: false,
                back: false
            }
        };
    } catch (error) {
        console.error("Error fetching application list:", error);
        // Return a default empty response instead of null to match the expected return type
        return {
            data: [],
            pagination: {
                page: 1,
                size: 10,
                totalCount: 0,
                totalItems: 0,
                totalPages: 0,
                pageCount: 0,
                orderField: ['name'],
                orderType: ['asc'],
                query: {},
                next: false,
                back: false
            }
        };
    }
}

async function listEventsToService(payload: PaginationParams): Promise<PaginatedApiResponse<any>> {
    try {
        const response = await fetchDataWithToken(
            servicesToEventsEndpoint,
            {
                page: payload.page,
                size: payload.size,
                order_field: payload.orderField,
                order_type: payload.orderType,
                query: payload.query,
            },
            "POST",
            false,
        );
        if (response?.status === 200 || response?.status === 202) {
            const responseData = response.data as PaginatedApiResponse<any>;
            return {
                data: responseData.data || [],
                pagination: {
                    page: responseData.pagination?.page || 1,
                    size: responseData.pagination?.size || 10,
                    totalCount: responseData.pagination?.totalCount || 0,
                    totalItems: responseData.pagination?.totalItems || 0,
                    totalPages: responseData.pagination?.totalPages || 0,
                    pageCount: responseData.pagination?.pageCount || 0,
                    orderField: responseData.pagination?.orderField || ['name'],
                    orderType: responseData.pagination?.orderType || ['asc'],
                    query: responseData.pagination?.query || {},
                    next: responseData.pagination?.next || false,
                    back: responseData.pagination?.back || false
                }
            };
        }
        return {
            data: [],
            pagination: {
                page: 1,
                size: 10,
                totalCount: 0,
                totalItems: 0,
                totalPages: 0,
                pageCount: 0,
                orderField: ['name'],
                orderType: ['asc'],
                query: {},
                next: false,
                back: false
            }
        };
    } catch (error) {
        console.error("Error fetching application list:", error);
        // Return a default empty response instead of null to match the expected return type
        return {
            data: [],
            pagination: {
                page: 1,
                size: 10,
                totalCount: 0,
                totalItems: 0,
                totalPages: 0,
                pageCount: 0,
                orderField: ['name'],
                orderType: ['asc'],
                query: {},
                next: false,
                back: false
            }
        };
    }
}

export {
    listServices,
    listEventsToService,
};
