import { request } from 'umi';
import { api } from '../api';


/**
 * 查询优化目标权限
 * @param data 
 * @returns 
 */
export async function getOptimizationGoalPermissionsV3Api(data: any) {
    return request(api + `/adq/v3/launch/tools/getOptimizationGoalPermissions`, {
        method: 'POST',
        data
    })
}

/**
 * 获取地理位置
 * @param data 
 * @returns 
 */
export async function getTargetingGagsApi(data: any) {
    return request(api + `/adq/v3/launch/tools/getTargetingGags`, {
        method: 'POST',
        data
    })
}


/**
 * 获取小说列表
 * @param data
 * @returns
 */
export async function getmarketingAssetContentApi(data: {
    marketingAssetType?: string,
    marketingAssetName?: string,
    accountId: number,
    pageNum: number,
    pageSize: number,
    marketingAssetNameList: string[]
}) {
    return request(api + `/adq/v3/marketingAssets/marketingAssetContent/get`, {
        method: 'POST',
        data,
    });
}

/**
 * 远程小说接口
 * @param data 
 * @returns 
 */
export async function getByRemotemarketingAssetContentApi(data: {
    marketingAssetType?: string,
    marketingAssetName?: string,
    accountId: number,
    pageNum: number,
    pageSize: number,
    marketingIdList: number[]
}) {
    return request(api + `/adq/v3/marketingAssets/marketingAssetContent/getByRemote`, {
        method: 'POST',
        data,
    });
}

/**
 * 同步小说
 * @param data
 * @returns
 */
export async function synMarketingAssetContentApi(data: { accountId: number }) {
    return request(api + `/adq/v3/marketingAssets/marketingAssetContent/sync`, {
        method: 'POST',
        data,
    });
}

/**
 * 获取公众号
 * @param data 
 * @returns 
 */
export async function getWechatOfficialAccountApi(data: { accountId: number }) {
    return request(api + `/adq/v3/marketingAssets/getWechatOfficialAccounts`, {
        method: 'POST',
        data,
    });
}

/**
 * 批量获取公众号
 * @param data 
 * @returns 
 */
export async function getWechatOfficialAccountsApi(data: { accountIdList: number[] }) {
    return request(api + `/adq/v3/marketingAssets/getWechatOfficialAccountsBatch`, {
        method: 'POST',
        data,
    });
}

/**
 * 获取视频号
 * @param data 
 * @returns 
 */
export async function getVideoChannelInfoApi(data: { accountIdList?: number[], accountId?: number }) {
    return request(api + `/adq/v3/marketingAssets/getVideoChannelInfo`, {
        method: 'POST',
        data,
    });
}

/**
 * 批量获取视频号
 * @param data 
 * @returns 
 */
export async function getVideoChannelInfoBatchApi(data: { accountIdList?: number[], accountId?: number }) {
    return request(api + `/adq/v3/marketingAssets/getVideoChannelInfoBatch`, {
        method: 'POST',
        data,
    });
}

/**
 * 获取转化归因
 * @param data 
 * @returns 
 */
export async function getConversionInfoApi(data: {
    accountId: number,
    pageNum: number,
    pageSize: number,
    accessStatus?: string[] // ACCESS_STATUS_COMPLETED 已接入 ACCESS_STATUS_PENDING 待接入
    conversionId?: number,
    conversionName?: string,
    conversionType?: string, // 扣费类型(ID_TYPE_CPC, BID_TYPE_CPA, BID_TYPE_CPS, BID_TYPE_CPM, BID_TYPE_CPD)
    createSourceType?: string,  // 转化创建来源，如 （SELF_CREATED 自建转化),(PLATFORM 平台类转化)
    deepBehaviorOptimizationGoal?: string,
    deepWorthAdvancedGoal?: string,
    deepWorthOptimizationGoal?: string,
    marketingScene?: string,
    optimizationGoal?: string,
    siteSet?: string,
}) {
    return request(api + `/adq/v3/launch/tools/getConversionInfo`, {
        method: 'POST',
        data,
    });
}


/**
 * 获取数据源
 * @param data
 * @returns
 */
export async function getDataSourceV3Api(data: { accountIds: number[], promotedObjectType?: string }) {
    return request(api + `/adq/v3/userActionSets/allByAccount`, {
        method: 'POST',
        data
    })
}

/**
 * 同步数据源
 * @param data
 * @returns
 */
export async function sysDataSourceV3Api(data: number[]) {
    return request(api + `/adq/v3/userActionSets/syncByAdAccountId`, {
        method: 'PATCH',
        data,
    });
}


/**
 * 获取创意规格详情
 * @param data 
 * @returns 
 */
export async function getCreativeDetailsApi(data: any) {
    return request(api + `/adq/v3/launch/tools/getCreativeDetails`, {
        method: 'POST',
        data
    })
}


/**
 * 获取流量包
 * @param data 
 * @returns 
 */
export async function getBatchUnionPositionPackagesApi(data: { unionPackageType: 'UNION_PACKAGE_TYPE_INCLUDE' | 'UNION_PACKAGE_TYPE_EXCLUDE', accountId?: number, accountIds?: number[] }) {
    return request(api + `/adq/v3/marketingAssets/getBatchUnionPositionPackages`, {
        method: 'POST',
        data,
    });
}


/**
 * 获取创意规格列表
 * @param data 
 * @returns 
 */
export async function getCreativeTemplateListApi(data: any) {
    return request(api + `/adq/v3/launch/tools/getCreativeTemplateList`, {
        method: 'POST',
        data
    })
}


/**
 * 文案助手
 * @param params 
 * @returns 
 */
export async function getTextApi(params: { maxTextLength: number, adAccountId: number }) {
    return request(api + `/adq/v3/launch/tools/creative/tools/text`, {
        method: 'GET',
        params
    })

}

/**
 * 获取原生页授权方信息
 * @param accountId 
 * @returns 
 */
export async function getWXDownPageAuthInfoListApi(accountId: number) {
    return request(api + `/adq/v3/marketingAssets/listWXDownPageAuthInfo/${accountId}`, {
        method: 'GET'
    });
}

/**
 * 获取ADQ落地页列表
 * @param adgroupName 广告名称
 * @param promotedObjectType 广告类型
 * @param accountId 账户ID
 */
export async function getAdqLandingPageListApi(params: {
    pageNum: number;
    pageSize: number;
    accountId?: number;
    pageName?: string;
    pageType?: string;
    pageTemplateId?: string;
    pageStatus?: string;
    ownerUid?: number;
    creativeTemplateId?: number,
    marketingCarrierId?: string,
    marketingCarrierType?: string
    marketingGoal?: string,
    marketingSubGoal?: string,
    marketingTargetType?: string,
    siteSet?: string[],
}) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(api + '/adq/v3/marketingAssets/listWXDownPageToRemote', {
        method: 'POST',
        data: params,
    });
}

/**
 * 删除落地页
 * @param data 
 * @returns 
 */
export async function delPageApi(data: { accountId: number, pageIds: number[] }) {
    return request(api + `/adq/v3/marketingAssets/batchDelWXDownPage`, {
        method: 'DELETE',
        data
    });
}

/**
 * 灵鹊落地页
 * @param params 
 * @returns 
 */
export async function getAdqLandingPageOfficialListApi(params: {
    pageNum: number;
    pageSize: number;
    accountId?: number;
    creativeTemplateId?: number,
    marketingCarrierId?: string,
    marketingCarrierType?: string
    marketingGoal?: string,
    marketingSubGoal?: string,
    marketingTargetType?: string,
    siteSet?: string[],
    ownerUid?: number,
    pageIds?: number[],
    pageName?: string,
    pageStatus?: string,
    pageType?: string,
    userId?: number
}) {
    Object.keys(params).forEach(key => {
        if (!params[key as keyof typeof params]) {
            delete params[key as keyof typeof params]
        }
    })
    return request(api + '/adq/v3/marketingAssets/listDownPageToRemote', {
        method: 'POST',
        data: params,
    });
}

/**
 * 按账号同步落地页
 * @param adAccountId 本地ID
 */
export async function putAdqLandingPageApi(data: any) {
    return request(api + `/adq/v3/marketingAssets/syncAllWXDownPage`, {
        method: 'PUT',
        data
    });
}


/**
 * 获取品牌形象列表
 * @returns 
 */
export async function getSysBrandApi() {
    return request(api + `/adq/sysBrand/allOfUser`, {
        method: 'GET'
    })
}

/**
 * 新增品牌形象
 * @param data 
 * @returns 
 */
export async function addSysBrandApi(data: { name: string, brandImgUrl: string }) {
    return request(api + `/adq/sysBrand`, {
        method: 'POST',
        data
    })
}

/**
 * 修改品牌形象
 * @param data 
 * @returns 
 */
export async function editSysBrandApi(data: { name: string, brandImgUrl: string, sysBrandId: number }) {
    const { sysBrandId, ...params } = data
    return request(api + `/adq/sysBrand/${sysBrandId}`, {
        method: 'PUT',
        data: params
    })
}

/**
 * 删除品牌形象
 * @param data 
 * @returns 
 */
export async function delSysBrandApi(data: { sysBrandId: number }) {
    const { sysBrandId } = data
    return request(api + `/adq/sysBrand/${sysBrandId}`, {
        method: 'DELETE'
    })
}


/**
 * 新增头像昵称跳转页
 * @param data 
 * @returns 
 */
export async function addProfilesApi(data: { profileName: string, imageUrl: string, description: string }) {
    return request(api + `/adq/v3/marketingAssets/profiles/add`, {
        method: 'POST',
        data
    })
}

/**
 * 获取头像昵称跳转页
 * @param data 
 * @returns 
 */
export async function getProfilesApi(data: { profileName?: string }) {
    return request(api + `/adq/v3/marketingAssets/profiles/get`, {
        method: 'POST',
        data
    })
}

/**
 * 删除头像昵称跳转页
 * @param data 
 * @returns 
 */
export async function delProfilesApi(params: { id: number }) {
    return request(api + `/adq/v3/marketingAssets/deleteSysProfiles`, {
        method: 'DELETE',
        params
    })
}

/**
 * 获取创意审核详情
 * @param data 
 * @returns 
 */
export async function getCreativeReviewDetailApi({ accountId, dynamicCreativeIds }: { accountId: number, dynamicCreativeIds: number[] }) {
    return request(api + `/adq/adgroup/creativeReviewDetail`, {
        method: 'POST',
        data: dynamicCreativeIds,
        params: { accountId }
    })
}

/**
 * 查询元素组件申诉复审结果
 * @param data 
 * @returns 
 */
export async function getFsResultApi(data: { accountId: number, componentId: number, dynamicCreativeId: number, elementFingerPrint: string, elementId: number }) {
    return request(api + `/adq/adgroup/getResult`, {
        method: 'POST',
        data
    })
}

/**
 * 同步广告
 * @param data 
 * @returns 
 */
export async function syncAdInfoApi(data: { accountAdgroupMaps: string[], adgroupCreateLogId: number }) {
    return request(api + `/adq/syncAdInfo/syncByAdgroupIds`, {
        method: 'POST',
        data
    })
}

/**
 * 获取图片信息
 * @param data 
 * @returns 
 */
export async function getImagesInfoApi(data: { adAccountId: number, imageIds: number[], pageNum: number, pageSize: number }) {
    return request(api + `/adq/v3/marketingAssets/getImages`, {
        method: 'POST',
        data
    })
}

/**
 * 获取视频信息
 * @param data 
 * @returns 
 */
export async function getVideosInfoApi(data: { adAccountId: number, videoIds: number[] }) {
    return request(api + `/adq/v3/marketingAssets/getVideos`, {
        method: 'POST',
        data
    })
}



/**
 * 新增微信小程序
 * @param data 
 * @returns 
 */
export async function addWechatAppletApi(data: { appletId: string, appletName: string, appletLink: string, description: string }) {
    return request(api + `/adq/v3/wechatApplet/add`, {
        method: 'POST',
        data
    })
}

/**
 * 查询本地微信小程序列表
 * @param data 
 * @returns 
 */
export async function getWechatAppletApi(data: { pageNum: number, pageSize: number, appletName?: string, appletIdList?: string[], createBy?: number }) {
    return request(api + `/adq/v3/wechatApplet/list`, {
        method: 'POST',
        data
    })
}

/**
 * 删除微信小程序
 * @param data 
 * @returns 
 */
export async function delWechatAppletApi(id: number) {
    return request(api + `/adq/v3/wechatApplet/delById/${id}`, {
        method: 'DELETE'
    })
}

/**
 * 下载小程序上传模板
 * @returns 
 */
export async function downloadTemplateApi() {
    return request(api + `/adq/v3/wechatApplet/downloadTemplate`, {
        method: 'POST',
        responseType: 'blob'
    })
}

/**
 * 上传
 * @returns 
 */
export async function importExcelApi(data: any) {
    return request(api + `/adq/v3/wechatApplet/importExcel`, {
        method: 'POST',
        data
    })
}

/**
 * 修改微信小程序
 * @param data 
 * @returns 
 */
export async function updateWechatAppletApi(data: { appletId: string, appletName: string, appletLink: string, description: string, id: number }) {
    return request(api + `/adq/v3/wechatApplet/update`, {
        method: 'POST',
        data
    })
}


/**
 * 获取所有微信小程序列表
 * @param data 
 * @returns 
 */
export async function getWechatAppletAllApi(data: { appletName?: string, appletIdList?: string[] }) {
    return request(api + `/adq/v3/wechatApplet/listAll`, {
        method: 'POST',
        data
    })
}

/**
 * 获取详情
 * @param id 
 * @returns 
 */
export async function getWechatAppletDetailApi(id: number) {
    return request(api + `/adq/v3/wechatApplet/getById/${id}`, {
        method: 'GET'
    })
}




/**
 * 新增本地企微微信信息
 * @param data 
 * @returns 
 */
export async function addCorpWechatApi(data: { wechatId: string, wechatName: string }) {
    return request(api + `/adq/v3/corpWechat/add`, {
        method: 'POST',
        data
    })
}

/**
 * 查询本地企业微信列表
 * @param data 
 * @returns 
 */
export async function getCorpWechatApi(data: { pageNum: number, pageSize: number, wechatName?: string, wechatIdList?: string[] }) {
    return request(api + `/adq/v3/corpWechat/list`, {
        method: 'POST',
        data
    })
}



/**
 * 删除本地企业微信信息
 * @param data 
 * @returns 
 */
export async function delCorpWechatApi(id: number) {
    return request(api + `/adq/v3/corpWechat/delById/${id}`, {
        method: 'DELETE'
    })
}


/**
 * 获取所有微信小程序列表
 * @param data 
 * @returns 
 */
export async function getCorpWechatAllApi(data: { wechatName?: string, wechatIdList?: string[] }) {
    return request(api + `/adq/v3/corpWechat/listAll`, {
        method: 'POST',
        data
    })
}

/**
 * 获取详情
 * @param id 
 * @returns 
 */
export async function getCorpWechatDetailApi(id: number) {
    return request(api + `/adq/v3/corpWechat/getById/${id}`, {
        method: 'GET'
    })
}


/**
 * 获取广告标签
 * @param data 
 * @returns 
 */
export async function getAdLabelApi(data: { accountId?: number }) {
    return request(api + `/adq/v3/launch/tools/getAdLabel`, {
        method: 'POST',
        data
    })
}

/**
 * 获取游戏列表
 * @param data 
 * @returns 
 */
export async function getGameLibraryListApi(data: { pageNum: number, pageSize: number, gameName?: string, idList?: number[], gameType?: string }) {
    return request(api + `/adq/v3/gameLibrary/pageList`, {
        method: 'POST',
        data
    })
}

/**
 * 所有游戏列表
 * @param data 
 * @returns 
 */
export async function getGameLibraryApi(data: { gameName?: string, idList?: number[], gameType?: string }) {
    return request(api + `/adq/v3/gameLibrary/listAll`, {
        method: 'POST',
        data
    })
}

/**
 * 新增游戏
 * @param data 
 * @returns 
 */
export async function addGameApi(data: { appId: string, gameName: string, gameType: string, id?: number }) {
    return request(api + `/adq/v3/gameLibrary/add`, {
        method: 'POST',
        data
    })
}

/**
 * 删除游戏
 * @param id 
 * @returns 
 */
export async function delGameApi(id: number) {
    return request(api + `/adq/v3/gameLibrary/delById/${id}`, {
        method: 'DELETE'
    })
}

/**
 * 获取游戏详情
 * @param id 
 * @returns 
 */
export async function getGameLibraryDetailApi(id: number) {
    return request(api + `/adq/v3/gameLibrary/getById/${id}`, {
        method: 'GET'
    })
}

/**
 * 获取游戏详情
 * @param wxGameAppId 
 * @returns 
 */
export async function getGameLibraryAppIdDetailApi(wxGameAppId: number) {
    return request(api + `/adq/v3/gameLibrary/getByAppId/${wxGameAppId}`, {
        method: 'GET'
    })
}


/**
 * 获取弹幕
 * @param data 
 * @returns 
 */
export async function getBarrageRecommendListApi(params: { adAccountId?: number, taskType?: 'GAME' | 'NOVEL' }) {
    // return request(api + `/adq/v3/launch/tools/creative/tools/barrage`, {
    return request(api + `/adq/v3/launch/tools/creative/tools/barrageRecommend`, {
        method: 'GET',
        params
    })
}


/**
 * 账户资产组分页查询
 * @param params 
 * @returns 
 */
export async function getAccountAssetsGroupListApi(data: { accountGroupName?: string, authMainAccountIds?: number[], authType?: string, pageNum: number, pageSize: number }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/pageList`, {
        method: 'POST',
        data
    })
}

/**
 * 所有账户资产共享组
 * @param data 
 * @returns 
 */
export async function getAccountAssetsGroupListAllApi(data: { accountGroupName?: string, authMainAccountIds?: number[] }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/ListAll`, {
        method: 'POST',
        data
    })
}


/**
 * 账户资产组修改（只能改名字）
 * @param data 
 * @returns 
 */
export async function updateAccountAssetsGroupApi(data: { accountGroupId: number, accountGroupName: string }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/update`, {
        method: 'POST',
        data
    })
}

/**
 * 账户资产组取消授权
 * @param data 
 * @returns 
 */
export async function revokeAuthAccountAssetsGroupApi(data: { accountAssetsGroupId: number, accountId: number[], assetId?: number }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/revokeAuth`, {
        method: 'POST',
        data
    })
}

/**
 * 删除账户组
 * @param data 
 * @returns 
 */
export async function delAuthAccountAssetsGroupApi(data: { id: number }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/delete/${data.id}`, {
        method: 'DELETE',
    })
}

/**
 * 账户资产组创建
 * @param data 
 * @returns 
 */
export async function createAccountAssetsGroupApi(data: { accountGroupId?: number, accountGroupName: string, authMainAccountId: number, authType: string }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/create`, {
        method: 'POST',
        data
    })
}


/**
 * 查询被授权的账户
 * @param data 
 * @returns 
 */
export async function getAccountAssetsGroupAccountListApi(data: { groupId: number }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/authAccountList/${data.groupId}`, {
        method: 'GET'
    })
}


/**
 * 账户资产授权
 * @param data 
 * @returns 
 */
export async function authAccountAssetsGroupAccountApi(data: { accountAssetsGroupId: number, accountId: number[], assetId?: number, assetName?: string }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/auth`, {
        method: 'POST',
        data
    })
}

/**
 * 批量授权
 * @param data 
 * @returns 
 */
export async function batchAuthAccountAssetsGroupAccountApi(data: { accountAssetsGroupId: number, accountId: number[], assetsDTOS: { assetId: number, assetName: string }[] }) {
    return request(api + `/adq/v3/marketingAssets/accountAssetsGroup/batchAuth`, {
        method: 'POST',
        data
    })
}

/**
 * 同步素材
 * @param data 
 * @returns 
 */
export async function syncMediaImageApi({ type, ...data }: { accountIds: number[], createTimeMin: string, createTimeMax: string, type: 'image' | 'video' }) {
    if (type === 'image') {
        return request(api + `/adq/v3/mediaImage/syncImage`, {
            method: 'POST',
            data
        })
    }
    return request(api + `/adq/v3/mediaVideo/syncVideo`, {
        method: 'POST',
        data
    })
}


/**
 * 获取文案库列表
 * @param data 
 * @returns 
 */
export async function getCopyWritingListApi(data: { category?: string, content?: string, createBy?: number, pageNum: number, pageSize: number }) {
    return request(api + `/adq/copyWriting/pageList`, {
        method: 'POST',
        data
    })
}

/**
 * 新增修改文案
 * @param data 
 * @returns 
 */
export async function copyWritingApi(data: { category: string, contentList?: string[], content?: string, id?: number }) {
    if (data?.id) {
        return request(api + `/adq/copyWriting/update`, {
            method: 'POST',
            data
        })
    }
    return request(api + `/adq/copyWriting/addBatch`, {
        method: 'POST',
        data
    })
}

/**
 * 删除
 * @param id 
 * @returns 
 */
export async function delCopyWritingApi(id: number) {
    return request(api + `/adq/copyWriting/delById/${id}`, {
        method: 'DELETE',
    })
}

/**
 * 批量删除
 * @param ids 
 * @returns 
 */
export async function delsCopyWritingApi(ids: number[]) {
    return request(api + `/adq/copyWriting/batchDelByIds`, {
        method: 'DELETE',
        data: ids
    })
}

export type ApplicationTypeProps = 'IOS' | 'ANDROID'
export interface GetApplicationListProps {
    pageNum: number,
    pageSize: number,
    createBy?: number,
    applicationId?: string
    name?: string
    type?: ApplicationTypeProps
}

/**
 * 查询本地推广应用列表
 * @param data 
 * @returns 
 */
export async function getApplicationListApi(data: GetApplicationListProps) {
    return request(api + `/adq/promote/application/list`, {
        method: 'POST',
        data
    })
}

/**
 * 查询本地推广全部应用列表
 * @param data 
 * @returns 
 */
export async function getApplicationAllApi(data: Omit<GetApplicationListProps, 'pageNum' | 'pageSize'>) {
    return request(api + `/adq/promote/application/listAll`, {
        method: 'POST',
        data
    })
}

export interface AddApplicationProps {
    applicationId: number,
    name: string,
    type: ApplicationTypeProps
    id?: number
}

/**
 * 新增推广应用
 * @param data 
 * @returns 
 */
export async function addApplicationApi(data: AddApplicationProps) {
    return request(api + `/adq/promote/application/add`, {
        method: 'POST',
        data
    })
}


/**
 * 修改推广应用
 * @param data 
 * @returns 
 */
export async function updateApplicationApi(data: Required<AddApplicationProps>) {
    return request(api + `/adq/promote/application/update`, {
        method: 'POST',
        data
    })
}

/**
 * 删除本地推广应用
 * @param id 
 * @returns 
 */
export async function delApplicationApi(id: number) {
    return request(api + `/adq/promote/application/delById/${id}`, {
        method: 'DELETE',
    })
}

/**
 * 应用详情
 * @param id 
 * @returns 
 */
export async function getApplicationDetailApi(id: number) {
    return request(api + `/adq/promote/application/getById/${id}`, {
        method: 'GET'
    })
}