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 interface SystemMessagePushPayload {
    action_param?: string;
    action_type?: string;
    aggregate_type?: string;
    batch_size?: number;
    body?: string;
    message_type: "system";
    producer_event_type?: string;
    summary: string;
    target_scope: "all_registered_users" | "user_ids";
    title: string;
    user_ids?: string[];
}

export interface SystemMessagePushResponse {
    command_id: string;
    created: boolean;
    job_id: string;
    message_type: string;
    status: string;
    target_scope: string;
}

export function createSystemMessagePushFanout(payload: SystemMessagePushPayload): Promise<SystemMessagePushResponse> {
    const endpoint = API_ENDPOINTS.createFullServerNoticeFanout;
    return apiRequest<SystemMessagePushResponse, SystemMessagePushPayload>(
        apiEndpointPath(API_OPERATIONS.createFullServerNoticeFanout),
        {
            body: payload,
            method: endpoint.method,
        },
    );
}

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

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

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

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

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

function appScopeHeaders(appCode: string) {
    // APP 配置全部是强 App 隔离数据；显式固定请求头可避免切换 App 时全局请求上下文变化导致写入错误租户。
    return { "X-App-Code": String(appCode || "").trim().toLowerCase() };
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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