function main() {
  const EMAIL_ADDRESSES = [‘your-email@example.com’]; // Add multiple emails if needed
  const SUBJECT = 'Google Ads Alert: Disapproved Items Found';

  let disapprovedItems = {
    ads: [],
    keywords: [],
    audiences: []
  };

  // Check all active campaigns
  const campaigns = AdsApp.campaigns()
    .withCondition('Status = ENABLED')
    .get();

  while (campaigns.hasNext()) {
    const campaign = campaigns.next();

    // Check active ad groups in each campaign
    const adGroups = campaign.adGroups()
      .withCondition('Status = ENABLED')
      .get();

    while (adGroups.hasNext()) {
      const adGroup = adGroups.next();

      // Check active responsive search ads
      const ads = adGroup.ads()
        .withCondition('Status = ENABLED')
        .withCondition('Type = RESPONSIVE_SEARCH_AD')
        .get();

      while (ads.hasNext()) {
        const ad = ads.next();
        if (ad.getPolicyApprovalStatus() === 'DISAPPROVED') {
          disapprovedItems.ads.push({
            campaign: campaign.getName(),
            adGroup: adGroup.getName(),
            adId: ad.getId(),
            headlines: ad.getHeadlines(),
            descriptions: ad.getDescriptions(),
            disapprovalReason: ad.getPolicyTopics()
          });
        }
      }

      // Check active keywords
      const keywords = adGroup.keywords()
        .withCondition('Status = ENABLED')
        .get();

      while (keywords.hasNext()) {
        const keyword = keywords.next();
        if (keyword.getApprovalStatus() === 'DISAPPROVED') {
          disapprovedItems.keywords.push({
            campaign: campaign.getName(),
            adGroup: adGroup.getName(),
            keyword: keyword.getText(),
            matchType: keyword.getMatchType(),
            disapprovalReason: keyword.getDisapprovalReasons()
          });
        }
      }

      // Check active audiences
      try {
        const audiences = adGroup.targeting().audiences()
          .withCondition('Status = ENABLED') // Only active audiences
          .get();

        while (audiences.hasNext()) {
          const audience = audiences.next();
          if (audience.getApprovalStatus() === 'DISAPPROVED') {
            disapprovedItems.audiences.push({
              campaign: campaign.getName(),
              adGroup: adGroup.getName(),
              audienceType: audience.getAudienceType(),
              audienceName: audience.getName(),
              disapprovalReason: audience.getDisapprovalReasons()
            });
          }
        }
      } catch (e) {
        Logger.log('Error checking audiences: ' + e);
      }
    }
  }

  // Send email if any disapproved items found
  if (hasDisapprovedItems(disapprovedItems)) {
    let emailBody = 'The following items were found to be disapproved:\n\n';

    // Add sections for the three asset types
    const sections = [
      {items: disapprovedItems.ads, title: 'DISAPPROVED ADS', formatter: formatAd},
      {items: disapprovedItems.keywords, title: 'DISAPPROVED KEYWORDS', formatter: formatKeyword},
      {items: disapprovedItems.audiences, title: 'DISAPPROVED AUDIENCES', formatter: formatAudience}
    ];

    sections.forEach(section => {
      emailBody = addDisapprovedSection(emailBody, section.items, section.title, section.formatter);
    });

    EMAIL_ADDRESSES.forEach(email => {
      MailApp.sendEmail(email, SUBJECT, emailBody);
    });

    Logger.log('Email notification sent for disapproved items.');
  } else {
    Logger.log('No disapproved items found.');
  }
}

// Helper function to check if any disapproved items exist
function hasDisapprovedItems(items) {
  for (let category in items) {
    if (items[category].length > 0) {
      return true;
    }
  }
  return false;
}

// Formatting functions
function formatAd(ad) {
  let text = 'Campaign: ' + ad.campaign + '\n';
  text += 'Ad Group: ' + ad.adGroup + '\n';
  text += 'Ad ID: ' + ad.adId + '\n';
  text += 'Headlines: ' + JSON.stringify(ad.headlines) + '\n';
  text += 'Descriptions: ' + JSON.stringify(ad.descriptions) + '\n';
  text += 'Disapproval Reason: ' + JSON.stringify(ad.disapprovalReason) + '\n';
  return text;
}

function formatKeyword(keyword) {
  let text = 'Campaign: ' + keyword.campaign + '\n';
  text += 'Ad Group: ' + keyword.adGroup + '\n';
  text += 'Keyword: ' + keyword.keyword + '\n';
  text += 'Match Type: ' + keyword.matchType + '\n';
  text += 'Disapproval Reason: ' + JSON.stringify(keyword.disapprovalReason) + '\n';
  return text;
}

function formatAudience(audience) {
  let text = 'Campaign: ' + audience.campaign + '\n';
  text += 'Ad Group: ' + audience.adGroup + '\n';
  text += 'Audience Type: ' + audience.audienceType + '\n';
  text += 'Audience Name: ' + audience.audienceName + '\n';
  text += 'Disapproval Reason: ' + JSON.stringify(audience.disapprovalReason) + '\n';
  return text;
}

function addDisapprovedSection(emailBody, items, title, formatter) {
  let result = emailBody;
  if (items.length > 0) {
    result += '\n' + title + '\n';
    result += '='.repeat(title.length) + '\n\n';

    items.forEach(item => {
      result += formatter(item) + '\n---\n\n';
    });
  }
  return result;
}