import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type {
    ApiList,
    AppBannerDto,
    AppBannerPayload,
    AppPopupDto,
    AppPopupPayload,
    AppSplashScreenDto,
    AppSplashScreenPayload,
    AppVersionDto,
    AppVersionPayload,
    EntityId,
    ExploreTabDto,
    ExploreTabPayload,
    H5LinkConfigDto,
    H5LinkConfigPayload,
    H5LinkConfigUpdatePayload,
    PageQuery,
} from "@/shared/api/types";

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

export function updateH5Links(payload: H5LinkConfigUpdatePayload): Promise<ApiList<H5LinkConfigDto>> {
    const endpoint = API_ENDPOINTS.updateH5Links;
    return apiRequest<ApiList<H5LinkConfigDto>, H5LinkConfigUpdatePayload>(
        apiEndpointPath(API_OPERATIONS.updateH5Links),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

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

export function updateH5Link(key: EntityId, payload: H5LinkConfigPayload): Promise<H5LinkConfigDto> {
    const endpoint = API_ENDPOINTS.updateH5Link;
    return apiRequest<H5LinkConfigDto, H5LinkConfigPayload>(apiEndpointPath(API_OPERATIONS.updateH5Link, { key }), {
        body: payload,
        method: endpoint.method,
    });
}

export function deleteH5Link(key: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteH5Link;
    return apiRequest<{ deleted: boolean }>(apiEndpointPath(API_OPERATIONS.deleteH5Link, { key }), {
        method: endpoint.method,
    });
}

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

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

export function updateExploreTab(tabId: EntityId, payload: ExploreTabPayload): Promise<ExploreTabDto> {
    const endpoint = API_ENDPOINTS.updateExploreTab;
    return apiRequest<ExploreTabDto, ExploreTabPayload>(
        apiEndpointPath(API_OPERATIONS.updateExploreTab, { tab_id: tabId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteExploreTab(tabId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteExploreTab;
    return apiRequest<{ deleted: boolean }>(apiEndpointPath(API_OPERATIONS.deleteExploreTab, { tab_id: tabId }), {
        method: endpoint.method,
    });
}

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

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

export function updateBanner(bannerId: EntityId, payload: AppBannerPayload): Promise<AppBannerDto> {
    const endpoint = API_ENDPOINTS.updateBanner;
    return apiRequest<AppBannerDto, AppBannerPayload>(
        apiEndpointPath(API_OPERATIONS.updateBanner, { banner_id: bannerId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteBanner(bannerId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteBanner;
    return apiRequest<{ deleted: boolean }>(apiEndpointPath(API_OPERATIONS.deleteBanner, { banner_id: bannerId }), {
        method: endpoint.method,
    });
}

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

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

export function updateSplashScreen(splashId: EntityId, payload: AppSplashScreenPayload): Promise<AppSplashScreenDto> {
    const endpoint = API_ENDPOINTS.updateSplashScreen;
    return apiRequest<AppSplashScreenDto, AppSplashScreenPayload>(
        apiEndpointPath(API_OPERATIONS.updateSplashScreen, { splash_id: splashId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteSplashScreen(splashId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteSplashScreen;
    return apiRequest<{ deleted: boolean }>(
        apiEndpointPath(API_OPERATIONS.deleteSplashScreen, { splash_id: splashId }),
        {
            method: endpoint.method,
        },
    );
}

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

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

export function updatePopup(popupId: EntityId, payload: AppPopupPayload): Promise<AppPopupDto> {
    const endpoint = API_ENDPOINTS.updatePopup;
    return apiRequest<AppPopupDto, AppPopupPayload>(
        apiEndpointPath(API_OPERATIONS.updatePopup, { popup_id: popupId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deletePopup(popupId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deletePopup;
    return apiRequest<{ deleted: boolean }>(apiEndpointPath(API_OPERATIONS.deletePopup, { popup_id: popupId }), {
        method: endpoint.method,
    });
}

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

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

export function updateAppVersion(versionId: EntityId, payload: AppVersionPayload): Promise<AppVersionDto> {
    const endpoint = API_ENDPOINTS.updateAppVersion;
    return apiRequest<AppVersionDto, AppVersionPayload>(
        apiEndpointPath(API_OPERATIONS.updateAppVersion, { version_id: versionId }),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

export function deleteAppVersion(versionId: EntityId): Promise<{ deleted: boolean }> {
    const endpoint = API_ENDPOINTS.deleteAppVersion;
    return apiRequest<{ deleted: boolean }>(
        apiEndpointPath(API_OPERATIONS.deleteAppVersion, { version_id: versionId }),
        {
            method: endpoint.method,
        },
    );
}
