const fetch = require('node-fetch');
const tools = require('../../../tools');
const config =  require('../../../etc/config.json')
async function getPromotionInfo(promotion_id,mini_program_platform_id=config.dy) {
    try {
        const url = new URL('https://new-media-fx.qimao.com/api/promotion/info');
        url.searchParams.append('promotion_id', promotion_id);
        url.searchParams.append('env', '');
        url.searchParams.append('t', Date.now());

        // 请求头
        const dy_headers = {
            'accept': 'application/json, text/plain, */*',
            'app-external-id': '4c6e8d8709c14de9a4397b6cbb978f85',
            'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0NDI1MjU5LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0NTExNjU5LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQ0MjUyNTksIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.KFEAi-QJcMXzSYokQmtF2-RsdfjiSEIRBM34hBuDjLY',
            'project': 'reader_paid_dyminiapp',
            'signature': '5aeeeff44f700540bb47ed41301fcbceefc342101818c1157cf7ef9331973d6e',
            'target-id': '544180064332673816'
        };

        const wx_headers = {
            'accept': 'application/json, text/plain, */*',
            'app-external-id': '9a2737aabb8f11ef93f90293e6ac97a2',
            'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0NDI1MjU5LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0NTExNjU5LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQ0MjUyNTksIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.KFEAi-QJcMXzSYokQmtF2-RsdfjiSEIRBM34hBuDjLY',
            'project': 'reader_paid_wxminiapp',
            'signature': '5aeeeff44f700540bb47ed41301fcbceefc342101818c1157cf7ef9331973d6e',
            'target-id': '544180064332673816'
        };
        let headers = ""
        if(mini_program_platform_id==config.wx){
            headers = wx_headers
        }else if(mini_program_platform_id==config.dy){
            headers = dy_headers
        }
        headers['authorization'] = await tools.getQMToken()
        const response = await fetch(url.toString(), {
            method: 'GET',
            headers:headers
        });

        if (response.status === 401) {
            throw new Error('Unauthorized: Token may be expired or invalid');
        }

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        var data = await response.json();
        return data;
    } catch (error) {
        console.error('Error:', error);
        throw error;
    }
}

async function getPromotionList(promotion_name,mini_program_platform_id=config.dy) {
    try {
        const url = new URL('https://new-media-fx.qimao.com/api/promotion/list');
        url.searchParams.append('promotion_name', promotion_name);
        url.searchParams.append('page', '1');
        url.searchParams.append('page_size', '50');
        url.searchParams.append('env', '');
        url.searchParams.append('t', Date.now());
   
        // 请求头
        const dy_headers = {
            'accept': 'application/json, text/plain, */*',
            'app-external-id': '4c6e8d8709c14de9a4397b6cbb978f85',
            'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0NDI1MjU5LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0NTExNjU5LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQ0MjUyNTksIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.KFEAi-QJcMXzSYokQmtF2-RsdfjiSEIRBM34hBuDjLY',
            'project': 'reader_paid_dyminiapp',
            'signature': '545831eb8ed618a0d16a04f9553c6285e896eda5ff7b01df5e4e9ba6715fe149',
            'target-id': '544180064332673816'
        };

        const wx_headers = {
            'accept': 'application/json, text/plain, */*',
            'app-external-id': '9a2737aabb8f11ef93f90293e6ac97a2',
            'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzM0NDI1MjU5LCJDbGllbnRJUCI6IjIxOS43Ny4yMzYuMTgwIiwiQnVmZmVyVGltZSI6MCwiZXhwIjoxNzM0NTExNjU5LCJpc3MiOiJxbV9uZXdfbWVkaWFfZngiLCJuYmYiOjE3MzQ0MjUyNTksIkVudiI6InJlbGVhc2UiLCJJc1NpbXVsYXRpb25Mb2dpbiI6ZmFsc2V9.KFEAi-QJcMXzSYokQmtF2-RsdfjiSEIRBM34hBuDjLY',
            'project': 'reader_paid_wxminiapp',
            'signature': '545831eb8ed618a0d16a04f9553c6285e896eda5ff7b01df5e4e9ba6715fe149',
            'target-id': '544180064332673816'
        };

        let headers = ""
        if(mini_program_platform_id==config.wx){
            headers = wx_headers
        }else if(mini_program_platform_id==config.dy){
            headers = dy_headers
        }
        headers['authorization'] = await tools.getQMToken()

        const response = await fetch(url.toString(), {
            method: 'GET',
            headers:headers
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();

        if(data.code!=200){
            throw data
        }
        if(data.data.list.length<=0){
            throw data
        }

        let promotion_id = data.data.list[0].promotion_id
        let info = await getPromotionInfo(promotion_id,mini_program_platform_id)
        info.promotion_id = promotion_id
        return info;
    } catch (error) {
        console.error('Error:', error);
        return null;
    }
}

module.exports = { getPromotionList };