const { default: axios } = require('axios');

const META_API_VERSION = process.env.META_API_VERSION || 'v21.0';
const META_API_BASE_URL = `https://graph.facebook.com/${META_API_VERSION}`;
const META_ACCESS_TOKEN = process.env.META_ACCESS_TOKEN;
const AD_ACCOUNT_ID = process.env.META_AD_ACCOUNT_ID;

const metaApiRequest = async (endpoint, params = {}) => {
  try {
    const response = await axios.get(`${META_API_BASE_URL}/${endpoint}`, {
      params: {
        access_token: META_ACCESS_TOKEN,
        time_zone: 'America/New_York',
        ...params,
      },
    });
    return response.data;
  } catch (error) {
    console.error('Meta API Error:', {
      message: error.message,
      response: error.response?.data,
      endpoint,
    });
    throw error;
  }
};

const fetchCampaigns = async (since, until) => {
  const campaigns = await metaApiRequest(`act_${AD_ACCOUNT_ID}/campaigns`, {
    fields: [
      'id',
      'name',
      'status',
      'objective',
      'daily_budget',
      'lifetime_budget',
      'adsets{daily_budget,lifetime_budget}',
      `insights.time_range({'since':'${since}','until':'${until}'}).level(campaign){
        spend,
        impressions,
        clicks,
        ctr,
        reach,
        frequency,
        cpm,
        cpc,
        actions,
        action_values,
        cost_per_action_type
      }`,
    ].join(','),
    limit: 100,
  });

  return campaigns.data.filter(c => c.insights?.data?.[0]?.spend > 0);
};

const fetchAccountInsights = async (since, until) => {
  const accountInsights = await metaApiRequest(`act_${AD_ACCOUNT_ID}/insights`, {
    fields: 'reach,spend,impressions,clicks,ctr,cpm,actions,action_values',
    time_range: JSON.stringify({ since, until }),
  });

  return accountInsights.data[0] || null;
};

const updateCampaignBudget = async (campaignId, budget) => {
  try {
    const response = await axios.post(`${META_API_BASE_URL}/${campaignId}`, {
      access_token: META_ACCESS_TOKEN,
      daily_budget: budget * 100, // Convert to cents
    });
    return response.data;
  } catch (error) {
    console.error('Update campaign budget error:', error);
    throw error;
  }
};

const updateCampaignStatus = async (campaignId, action) => {
  try {
    const status = action === 'pause' ? 'PAUSED' : 'ACTIVE';
    const response = await axios.post(`${META_API_BASE_URL}/${campaignId}`, {
      access_token: META_ACCESS_TOKEN,
      status,
    });
    return response.data;
  } catch (error) {
    console.error('Update campaign status error:', error);
    throw error;
  }
};

module.exports = {
  fetchCampaigns,
  fetchAccountInsights,
  updateCampaignBudget,
  updateCampaignStatus,
};
