<?php

/**
 * 
 */
class MioCommerceOlympusDataClearer {

  public function clearExistingData() {
    // Suppression des contenus existants
    $this->clearServices();
    $this->clearTestimonials();
    $this->clearFaqs();
    $this->clearReusableBlocks();

    // Suppression des taxonomies
    $this->clearTaxonomies();

    // Suppression des pages
    $this->clearPages();

    $this->clearPosts();

    $this->clearGeneralSettings();
  }

  private function clearServices() {
    $allServices = get_posts([
      'post_type' => 'service',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    foreach ($allServices as $service) {
      wp_delete_post($service->ID, true);
    }
  }

  private function clearTestimonials() {
    $allTestimonials = get_posts([
      'post_type' => 'olympus-testimonials',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    foreach ($allTestimonials as $testimonial) {
      wp_delete_post($testimonial->ID, true);
    }
  }

  private function clearFaqs() {
    $allFaqs = get_posts([
      'post_type' => 'olympus-faq',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    foreach ($allFaqs as $faq) {
      wp_delete_post($faq->ID, true);
    }
  }

  private function clearReusableBlocks() {
    $allBlocks = get_posts([
      'post_type' => 'wp_block',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    foreach ($allBlocks as $block) {
      wp_delete_post($block->ID, true);
    }
  }

  private function clearTaxonomies() {
    // Nettoyage des catégories de service
    $serviceCategories = get_terms([
      'taxonomy' => 'service-category',
      'hide_empty' => false
    ]);

    foreach ($serviceCategories as $term) {
      wp_delete_term($term->term_id, 'service-category');
    }

    // Nettoyage des labels de service
    $serviceLabels = get_terms([
      'taxonomy' => 'service-label',
      'hide_empty' => false
    ]);

    foreach ($serviceLabels as $term) {
      wp_delete_term($term->term_id, 'service-label');
    }
  }

  private function clearPages() {
    // Récupérer toutes les pages
    $allPages = get_posts([
      'post_type' => 'page',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    // Supprimer chaque page
    foreach ($allPages as $page) {
      error_log("[Automator] Deleting page: " . $page->post_title);
      wp_delete_post($page->ID, true);
    }

    // Réinitialiser les options de la page d'accueil
    update_option('show_on_front', 'posts');
    update_option('page_on_front', 0);
  }

  private function clearPosts() {
    // Récupérer toutes les pages
    $allPosts = get_posts([
      'post_type' => 'post',
      'numberposts' => -1,
      'post_status' => 'any'
    ]);

    // Supprimer chaque page
    foreach ($allPosts as $post) {
      error_log("[Automator] Deleting page: " . $post->post_title);
      wp_delete_post($post->ID, true);
    }

    // Réinitialiser les options de la page d'accueil
    update_option('show_on_front', 'posts');
    update_option('page_on_front', 0);
  }


  private function clearGeneralSettings() {
    update_field('logo_main_nav', "", 'option');
    update_field('menu_items', "", 'option');
    update_field('menu_call_to_action', "", 'option');
    update_field('cta_has_submenu', 0, 'option');
    update_field('cta_submenu', "", 'option');
    update_field('logo_footer', "", 'option');
    update_field('f_menu_cols', "", 'option');
    update_field('floating_dock_script', "", 'option');
  }
}
