import '@/types/Shopee/Seller/Ads';
import moment from 'moment';
import { apiShopeeSeller } from '@/api/shopee/seller/init';

/**
 *
 * @param {number} limit
 * @param {number} page
 * @param {ShopeeAdsState} state
 * @param {ShopeeAdsTarget} type
 * @param {string} search
 * @returns {Promise<ShopeeAdsCampaignList[]>}
 */
export const getListAdsCampaign = async (limit = 10, page = 0, state = 'all', type = '', search = '') => {
  const payload = {
    campaign_state: state,
    sort_key: 'performance',
    sort_direction: '1',
    start_time: moment().subtract(12, 'months').unix(),
    end_time: moment().unix(),
    offset: page,
    limit,
  };

  if (search) {
    payload.campaign_type = 'search';
    payload.search_content = search;
  }

  if (type) {
    payload.campaign_type = type;
  }

  const result = await apiShopeeSeller.get('/api/marketing/v3/pas/homepage/', {
    params: payload,
  });

  return result.data.campaign_ads_list || [];
};

/**
 *
 * @param {number} campaginId
 * @returns {Promise<ShopeeAdsCampaignId>}
 */
export const getAdsCampaignId = async (campaginId) => {
  const result = await apiShopeeSeller.get('/api/marketing/v3/pas/campaign/', {
    params: {
      campaignid: campaginId,
    },
  });

  return result.data || null;
};

/**
 *
 * @param {number} campaginId
 * @param {ShopeeAdsCampaignKeyword} keywordPayload
 * @returns {Promise}
 */
export const addKeywordCampaignId = async (campaginId, keywordPayload) => {
  const payload = {
    campaignid: Number(campaginId),
    placement: 0,
    keyword_list: [keywordPayload],
  };
  const result = await apiShopeeSeller.put('/api/marketing/v3/pas/search/', payload);

  return result;
};

/**
 *
 * @param {number} campaginId
 * @param {ShopeeAdsCampaignKeyword} keywordPayload
 * @returns {Promise}
 */
export const updateKeywordCampaignId = async (campaginId, keywordPayload) => {
  const payload = {
    campaignid: Number(campaginId),
    placement: 0,
    keyword_list: [keywordPayload],
  };
  const result = await apiShopeeSeller.put('/api/marketing/v3/pas/search/', payload);

  return result;
};
