import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type {
    ApiPage,
    ApiList,
    EntityId,
    PageQuery,
    RechargeBillDto,
    RechargeProductDto,
    RechargeProductPayload,
    TemporaryPaymentLinkDto,
    ThirdPartyPaymentChannelDto,
    ThirdPartyPaymentMethodSyncDto,
    ThirdPartyPaymentMethodSyncPayload,
    ThirdPartyPaymentMethodDto,
    ThirdPartyPaymentRateSyncPayload,
    ThirdPartyPaymentRateSyncDto,
} from "@/shared/api/types";

export function listRechargeBills(query: PageQuery = {}): Promise<ApiPage<RechargeBillDto>> {
    const endpoint = API_ENDPOINTS.listRechargeBills;
    return apiRequest<ApiPage<RechargeBillDto>>(apiEndpointPath(API_OPERATIONS.listRechargeBills), {
        method: endpoint.method,
        query,
    });
}

export function listRechargeProducts(query: PageQuery = {}): Promise<ApiPage<RechargeProductDto>> {
    const endpoint = API_ENDPOINTS.listRechargeProducts;
    return apiRequest<ApiPage<RechargeProductDto>>(apiEndpointPath(API_OPERATIONS.listRechargeProducts), {
        method: endpoint.method,
        query,
    });
}

export function createRechargeProduct(payload: RechargeProductPayload): Promise<RechargeProductDto> {
    const endpoint = API_ENDPOINTS.createRechargeProduct;
    return apiRequest<RechargeProductDto, RechargeProductPayload>(
        apiEndpointPath(API_OPERATIONS.createRechargeProduct),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function updateRechargeProduct(
    productId: EntityId,
    payload: RechargeProductPayload,
): Promise<RechargeProductDto> {
    const endpoint = API_ENDPOINTS.updateRechargeProduct;
    return apiRequest<RechargeProductDto, RechargeProductPayload>(
        apiEndpointPath(API_OPERATIONS.updateRechargeProduct, { product_id: productId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteRechargeProduct(productId: EntityId): Promise<unknown> {
    const endpoint = API_ENDPOINTS.deleteRechargeProduct;
    return apiRequest(apiEndpointPath(API_OPERATIONS.deleteRechargeProduct, { product_id: productId }), {
        method: endpoint.method,
    });
}

export function listThirdPartyPaymentChannels(): Promise<ApiList<ThirdPartyPaymentChannelDto>> {
    const endpoint = API_ENDPOINTS.listThirdPartyPaymentChannels;
    return apiRequest<ApiList<ThirdPartyPaymentChannelDto>>(
        apiEndpointPath(API_OPERATIONS.listThirdPartyPaymentChannels),
        {
            method: endpoint.method,
        },
    );
}

export function listTemporaryPaymentLinks(query: PageQuery = {}): Promise<ApiPage<TemporaryPaymentLinkDto>> {
    const endpoint = API_ENDPOINTS.listTemporaryPaymentLinks;
    return apiRequest<ApiPage<TemporaryPaymentLinkDto>>(apiEndpointPath(API_OPERATIONS.listTemporaryPaymentLinks), {
        method: endpoint.method,
        query,
    });
}

export function getTemporaryPaymentLink(orderId: EntityId): Promise<TemporaryPaymentLinkDto> {
    const endpoint = API_ENDPOINTS.getTemporaryPaymentLink;
    return apiRequest<TemporaryPaymentLinkDto>(
        apiEndpointPath(API_OPERATIONS.getTemporaryPaymentLink, { order_id: orderId }),
        {
            method: endpoint.method,
        },
    );
}

export function setThirdPartyPaymentMethodStatus(
    methodId: EntityId,
    payload: { enabled: boolean },
): Promise<ThirdPartyPaymentMethodDto> {
    const endpoint = API_ENDPOINTS.setThirdPartyPaymentMethodStatus;
    return apiRequest<ThirdPartyPaymentMethodDto, { enabled: boolean }>(
        apiEndpointPath(API_OPERATIONS.setThirdPartyPaymentMethodStatus, { method_id: methodId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function updateThirdPartyPaymentRate(
    methodId: EntityId,
    payload: { usdToCurrencyRate: string },
): Promise<ThirdPartyPaymentMethodDto> {
    const endpoint = API_ENDPOINTS.updateThirdPartyPaymentRate;
    return apiRequest<ThirdPartyPaymentMethodDto, { usdToCurrencyRate: string }>(
        apiEndpointPath(API_OPERATIONS.updateThirdPartyPaymentRate, { method_id: methodId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function syncThirdPartyPaymentMethods(
    payload: ThirdPartyPaymentMethodSyncPayload = { providerCode: "v5pay" },
): Promise<ThirdPartyPaymentMethodSyncDto> {
    const endpoint = API_ENDPOINTS.syncThirdPartyPaymentMethods;
    return apiRequest<ThirdPartyPaymentMethodSyncDto, ThirdPartyPaymentMethodSyncPayload>(
        apiEndpointPath(API_OPERATIONS.syncThirdPartyPaymentMethods),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function syncThirdPartyPaymentRates(
    payload: ThirdPartyPaymentRateSyncPayload = { markupPercent: 0 },
): Promise<ThirdPartyPaymentRateSyncDto> {
    const endpoint = API_ENDPOINTS.syncThirdPartyPaymentRates;
    return apiRequest<ThirdPartyPaymentRateSyncDto, ThirdPartyPaymentRateSyncPayload>(
        apiEndpointPath(API_OPERATIONS.syncThirdPartyPaymentRates),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}
