-- ATTENTION: Utiliser ce squelette avec prudence et uniquement si la réinstallation n'est pas possible.
-- Objectif: nettoyer la base pour revenir à un état proche post-install sans données démo.
-- Sauvegardez impérativement la DB avant exécution.
-- Consolidated reset script for Magento Open Source 2.4.7
-- Auteur: LCA Network
-- ATTENTION: A utiliser uniquement en environnement de développement/recette. Sauvegardez la base avant.
-- Objectif: nettoyer Clients, Ventes, Devis, Catalogue (produits), Catégories (sauf root/default), Règles, Réécritures, Rapports, Inventaire MSI (sélectif), Séquences.
-- IMPORTANT: Ne supprime PAS le contenu CMS (cms_page, cms_block, etc.)

SET FOREIGN_KEY_CHECKS=0;

/* ============================================================
   1) CLIENTS ET COMPTES
   ============================================================ */
TRUNCATE TABLE `customer_address_entity`;
TRUNCATE TABLE `customer_address_entity_datetime`;
TRUNCATE TABLE `customer_address_entity_decimal`;
TRUNCATE TABLE `customer_address_entity_int`;
TRUNCATE TABLE `customer_address_entity_text`;
TRUNCATE TABLE `customer_address_entity_varchar`;
TRUNCATE TABLE `customer_entity`;
TRUNCATE TABLE `customer_entity_datetime`;
TRUNCATE TABLE `customer_entity_decimal`;
TRUNCATE TABLE `customer_entity_int`;
TRUNCATE TABLE `customer_entity_text`;
TRUNCATE TABLE `customer_entity_varchar`;
TRUNCATE TABLE `customer_grid_flat`;
TRUNCATE TABLE `customer_log`;
TRUNCATE TABLE `customer_visitor`;
TRUNCATE TABLE `persistent_session`;
TRUNCATE TABLE `product_alert_price`;
TRUNCATE TABLE `product_alert_stock`;
TRUNCATE TABLE `vault_payment_token`;
TRUNCATE TABLE `vault_payment_token_order_payment_link`;

ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_grid_flat` AUTO_INCREMENT=1;
ALTER TABLE `customer_log` AUTO_INCREMENT=1;
ALTER TABLE `customer_visitor` AUTO_INCREMENT=1;
ALTER TABLE `persistent_session` AUTO_INCREMENT=1;
ALTER TABLE `product_alert_price` AUTO_INCREMENT=1;
ALTER TABLE `product_alert_stock` AUTO_INCREMENT=1;
ALTER TABLE `vault_payment_token` AUTO_INCREMENT=1;
ALTER TABLE `vault_payment_token_order_payment_link` AUTO_INCREMENT=1;

/* ============================================================
   2) WISHLIST / NEWSLETTER / RAPPORTS CLIENTS
   ============================================================ */
TRUNCATE TABLE `wishlist`;
TRUNCATE TABLE `wishlist_item`;
TRUNCATE TABLE `wishlist_item_option`;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `wishlist_item` AUTO_INCREMENT=1;
ALTER TABLE `wishlist_item_option` AUTO_INCREMENT=1;

TRUNCATE TABLE `newsletter_subscriber`;
TRUNCATE TABLE `newsletter_queue`;
TRUNCATE TABLE `newsletter_queue_link`;
TRUNCATE TABLE `newsletter_queue_store_link`;
TRUNCATE TABLE `newsletter_problem`;
ALTER TABLE `newsletter_subscriber` AUTO_INCREMENT=1;

TRUNCATE TABLE `report_event`;
TRUNCATE TABLE `report_compared_product_index`;
TRUNCATE TABLE `report_viewed_product_aggregated_daily`;
TRUNCATE TABLE `report_viewed_product_aggregated_monthly`;
TRUNCATE TABLE `report_viewed_product_aggregated_yearly`;
TRUNCATE TABLE `report_viewed_product_index`;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
ALTER TABLE `report_compared_product_index` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_aggregated_daily` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_aggregated_monthly` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_aggregated_yearly` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_index` AUTO_INCREMENT=1;

/* ============================================================
   3) CATEGORIES (conserver root=1 et default=2)
   ============================================================ */
-- Nettoyer d'abord les valeurs EAV de toutes les catégories autres que 1 et 2
DELETE FROM `catalog_category_entity_varchar`
  WHERE `entity_id` NOT IN (1,2);
DELETE FROM `catalog_category_entity_int`
  WHERE `entity_id` NOT IN (1,2);
DELETE FROM `catalog_category_entity_decimal`
  WHERE `entity_id` NOT IN (1,2);
DELETE FROM `catalog_category_entity_datetime`
  WHERE `entity_id` NOT IN (1,2);
DELETE FROM `catalog_category_entity_text`
  WHERE `entity_id` NOT IN (1,2);

-- Supprimer ensuite toutes les catégories sauf root (1) et default (2)
DELETE FROM `catalog_category_entity` WHERE `entity_id` NOT IN (1,2);

TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `catalog_category_product_index`;
TRUNCATE TABLE `catalog_category_product_index_tmp`;
TRUNCATE TABLE `catalog_category_product_index_store1`;
TRUNCATE TABLE `catalog_category_product_index_store1_replica`;
ALTER TABLE `catalog_category_product` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_product_index` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_product_index_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_product_index_store1` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_product_index_store1_replica` AUTO_INCREMENT=1;

/* ============================================================
   4) CATALOGUE / PRODUITS
   ============================================================ */
TRUNCATE TABLE `catalog_compare_item`;
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_price_index`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_bundle_selection_price`;
TRUNCATE TABLE `catalog_product_bundle_stock_index`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value_to_entity`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value_video`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_index_eav`;
TRUNCATE TABLE `catalog_product_index_eav_decimal`;
TRUNCATE TABLE `catalog_product_index_eav_decimal_idx`;
TRUNCATE TABLE `catalog_product_index_eav_decimal_tmp`;
TRUNCATE TABLE `catalog_product_index_eav_idx`;
TRUNCATE TABLE `catalog_product_index_eav_tmp`;
TRUNCATE TABLE `catalog_product_index_price`;
TRUNCATE TABLE `catalog_product_index_price_bundle_idx`;
TRUNCATE TABLE `catalog_product_index_price_bundle_opt_idx`;
TRUNCATE TABLE `catalog_product_index_price_bundle_opt_tmp`;
TRUNCATE TABLE `catalog_product_index_price_bundle_sel_idx`;
TRUNCATE TABLE `catalog_product_index_price_bundle_sel_tmp`;
TRUNCATE TABLE `catalog_product_index_price_bundle_tmp`;
TRUNCATE TABLE `catalog_product_index_price_cfg_opt_agr_idx`;
TRUNCATE TABLE `catalog_product_index_price_cfg_opt_agr_tmp`;
TRUNCATE TABLE `catalog_product_index_price_cfg_opt_idx`;
TRUNCATE TABLE `catalog_product_index_price_cfg_opt_tmp`;
TRUNCATE TABLE `catalog_product_index_price_downlod_idx`;
TRUNCATE TABLE `catalog_product_index_price_downlod_tmp`;
TRUNCATE TABLE `catalog_product_index_price_final_idx`;
TRUNCATE TABLE `catalog_product_index_price_final_tmp`;
TRUNCATE TABLE `catalog_product_index_price_idx`;
TRUNCATE TABLE `catalog_product_index_price_opt_agr_idx`;
TRUNCATE TABLE `catalog_product_index_price_opt_agr_tmp`;
TRUNCATE TABLE `catalog_product_index_price_opt_idx`;
TRUNCATE TABLE `catalog_product_index_price_opt_tmp`;
TRUNCATE TABLE `catalog_product_index_price_tmp`;
TRUNCATE TABLE `catalog_product_index_tier_price`;
TRUNCATE TABLE `catalog_product_index_website`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_relation`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_url_rewrite_product_category`;
TRUNCATE TABLE `downloadable_link`;
TRUNCATE TABLE `downloadable_link_price`;
TRUNCATE TABLE `downloadable_link_purchased`;
TRUNCATE TABLE `downloadable_link_purchased_item`;
TRUNCATE TABLE `downloadable_link_title`;
TRUNCATE TABLE `downloadable_sample`;
TRUNCATE TABLE `downloadable_sample_title`;

ALTER TABLE `catalog_compare_item` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_option` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_option_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_price_index` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_selection` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_selection_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_bundle_stock_index` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_gallery` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery_value_to_entity` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_media_gallery_value_video` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_tier_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav_decimal_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav_decimal_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_eav_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_opt_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_opt_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_sel_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_sel_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_bundle_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_cfg_opt_agr_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_cfg_opt_agr_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_cfg_opt_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_cfg_opt_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_downlod_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_downlod_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_final_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_final_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_opt_agr_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_opt_agr_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_opt_idx` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_opt_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_price_tmp` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_tier_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_index_website` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_int` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_link_attribute_varchar` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_title` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_price` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_title` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_option_type_value` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_relation` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_attribute` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_attribute_label` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_super_link` AUTO_INCREMENT=1;
ALTER TABLE `catalog_product_website` AUTO_INCREMENT=1;
ALTER TABLE `catalog_url_rewrite_product_category` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_price` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_purchased` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_purchased_item` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_title` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_sample` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_sample_title` AUTO_INCREMENT=1;

/* ============================================================
   5) INVENTAIRE LEGACY & MSI (sélectif)
   ============================================================ */
-- Legacy
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
TRUNCATE TABLE `cataloginventory_stock_status_idx`;
TRUNCATE TABLE `cataloginventory_stock_status_tmp`;
ALTER TABLE `cataloginventory_stock_item` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock_status` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock_status_idx` AUTO_INCREMENT=1;
ALTER TABLE `cataloginventory_stock_status_tmp` AUTO_INCREMENT=1;

-- MSI: NE PAS supprimer les sources/stocks de base. On vide seulement les réservations et items.
TRUNCATE TABLE `inventory_reservation`;
TRUNCATE TABLE `inventory_source_item`;
-- ALTER TABLE `inventory_reservation` AUTO_INCREMENT=1; -- (pas d'AUTO_INCREMENT)
-- ALTER TABLE `inventory_source_item` AUTO_INCREMENT=1; -- (pas d'AUTO_INCREMENT)

/* ============================================================
   6) URL REWRITES
   ============================================================ */
TRUNCATE TABLE `url_rewrite`;

/* ============================================================
   7) AVIS / NOTES PRODUIT
   ============================================================ */
TRUNCATE TABLE `rating_option_vote`;
TRUNCATE TABLE `rating_option_vote_aggregated`;
TRUNCATE TABLE `review`;
TRUNCATE TABLE `review_detail`;
TRUNCATE TABLE `review_entity_summary`;
TRUNCATE TABLE `review_store`;
ALTER TABLE `review` AUTO_INCREMENT=1;
ALTER TABLE `review_detail` AUTO_INCREMENT=1;
ALTER TABLE `review_entity_summary` AUTO_INCREMENT=1;
ALTER TABLE `review_store` AUTO_INCREMENT=1;
ALTER TABLE `rating_option_vote` AUTO_INCREMENT=1;
ALTER TABLE `rating_option_vote_aggregated` AUTO_INCREMENT=1;

/* ============================================================
   8) DEVIS / VENTES / PAIEMENTS
   ============================================================ */
TRUNCATE TABLE `gift_message`;
TRUNCATE TABLE `quote`;
TRUNCATE TABLE `quote_address`;
TRUNCATE TABLE `quote_address_item`;
TRUNCATE TABLE `quote_id_mask`;
TRUNCATE TABLE `quote_item`;
TRUNCATE TABLE `quote_item_option`;
TRUNCATE TABLE `quote_payment`;
TRUNCATE TABLE `quote_shipping_rate`;
TRUNCATE TABLE `reporting_orders`;
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;
TRUNCATE TABLE `sales_creditmemo`;
TRUNCATE TABLE `sales_creditmemo_comment`;
TRUNCATE TABLE `sales_creditmemo_grid`;
TRUNCATE TABLE `sales_creditmemo_item`;
TRUNCATE TABLE `sales_invoice`;
TRUNCATE TABLE `sales_invoiced_aggregated`;
TRUNCATE TABLE `sales_invoiced_aggregated_order`;
TRUNCATE TABLE `sales_invoice_comment`;
TRUNCATE TABLE `sales_invoice_grid`;
TRUNCATE TABLE `sales_invoice_item`;
TRUNCATE TABLE `sales_order`;
TRUNCATE TABLE `sales_order_address`;
TRUNCATE TABLE `sales_order_aggregated_created`;
TRUNCATE TABLE `sales_order_aggregated_updated`;
TRUNCATE TABLE `sales_order_grid`;
TRUNCATE TABLE `sales_order_item`;
TRUNCATE TABLE `sales_order_payment`;
TRUNCATE TABLE `sales_order_status_history`;
TRUNCATE TABLE `sales_order_tax`;
TRUNCATE TABLE `sales_order_tax_item`;
TRUNCATE TABLE `sales_payment_transaction`;
TRUNCATE TABLE `sales_refunded_aggregated`;
TRUNCATE TABLE `sales_refunded_aggregated_order`;
TRUNCATE TABLE `sales_shipment`;
TRUNCATE TABLE `sales_shipment_comment`;
TRUNCATE TABLE `sales_shipment_grid`;
TRUNCATE TABLE `sales_shipment_item`;
TRUNCATE TABLE `sales_shipment_track`;
TRUNCATE TABLE `sales_shipping_aggregated`;
TRUNCATE TABLE `sales_shipping_aggregated_order`;
TRUNCATE TABLE `tax_order_aggregated_created`;
TRUNCATE TABLE `tax_order_aggregated_updated`;
TRUNCATE TABLE `paypal_payment_transaction`;
TRUNCATE TABLE `paypal_settlement_report`;
TRUNCATE TABLE `paypal_settlement_report_row`;

ALTER TABLE `gift_message` AUTO_INCREMENT=1;
ALTER TABLE `quote` AUTO_INCREMENT=1;
ALTER TABLE `quote_address` AUTO_INCREMENT=1;
ALTER TABLE `quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `quote_id_mask` AUTO_INCREMENT=1;
ALTER TABLE `quote_item` AUTO_INCREMENT=1;
ALTER TABLE `quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `reporting_orders` AUTO_INCREMENT=1;
ALTER TABLE `sales_bestsellers_aggregated_daily` AUTO_INCREMENT=1;
ALTER TABLE `sales_bestsellers_aggregated_monthly` AUTO_INCREMENT=1;
ALTER TABLE `sales_bestsellers_aggregated_yearly` AUTO_INCREMENT=1;
ALTER TABLE `sales_creditmemo` AUTO_INCREMENT=1;
ALTER TABLE `sales_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_aggregated_updated` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_tax` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_tax_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_payment_transaction` AUTO_INCREMENT=1;
ALTER TABLE `sales_refunded_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_refunded_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipping_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_shipping_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `tax_order_aggregated_created` AUTO_INCREMENT=1;
ALTER TABLE `tax_order_aggregated_updated` AUTO_INCREMENT=1;
ALTER TABLE `paypal_payment_transaction` AUTO_INCREMENT=1;
ALTER TABLE `paypal_settlement_report` AUTO_INCREMENT=1;
ALTER TABLE `paypal_settlement_report_row` AUTO_INCREMENT=1;

/* ============================================================
   9) REGLES PROMO (VENTES & CATALOGUE)
   ============================================================ */
-- Sales rules
TRUNCATE TABLE `salesrule`;
TRUNCATE TABLE `salesrule_coupon`;
TRUNCATE TABLE `salesrule_coupon_aggregated`;
TRUNCATE TABLE `salesrule_coupon_aggregated_order`;
TRUNCATE TABLE `salesrule_coupon_aggregated_updated`;
TRUNCATE TABLE `salesrule_coupon_usage`;
TRUNCATE TABLE `salesrule_customer`;
TRUNCATE TABLE `salesrule_customer_group`;
TRUNCATE TABLE `salesrule_label`;
TRUNCATE TABLE `salesrule_product_attribute`;
TRUNCATE TABLE `salesrule_website`;

-- Catalog rules
TRUNCATE TABLE `catalogrule`;
TRUNCATE TABLE `catalogrule_product`;
TRUNCATE TABLE `catalogrule_group_website`;
TRUNCATE TABLE `catalogrule_website`;
TRUNCATE TABLE `catalogrule_product_price`;

/* ============================================================
   10) RECHERCHE & DIVERS (optionnel)
   ============================================================ */
-- Facultatif selon votre moteur de recherche/config
-- TRUNCATE TABLE `search_query`;
-- TRUNCATE TABLE `catalogsearch_fulltext_scope1`;

/* ============================================================
   11) SEQUENCES (IDs de commande, facture, etc.)
   ============================================================ */
-- Troncature dynamique des tables de séquence par type (via procédure temporaire pour éviter multi-statements en PREPARE)
SET @db = DATABASE();

DELIMITER //
DROP PROCEDURE IF EXISTS lca_truncate_sequences//
CREATE PROCEDURE lca_truncate_sequences(IN like_pattern VARCHAR(128))
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE t VARCHAR(255);
  DECLARE cur CURSOR FOR
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = @db AND table_name LIKE like_pattern ESCAPE '\\';
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur;
  read_loop: LOOP
    FETCH cur INTO t;
    IF done THEN
      LEAVE read_loop;
    END IF;
    SET @sql = CONCAT('TRUNCATE TABLE `', t, '`');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
  END LOOP;
  CLOSE cur;
END//
DELIMITER ;

CALL lca_truncate_sequences('sequence\\_order\\_%');
CALL lca_truncate_sequences('sequence\\_invoice\\_%');
CALL lca_truncate_sequences('sequence\\_shipment\\_%');
CALL lca_truncate_sequences('sequence\\_creditmemo\\_%');

DROP PROCEDURE IF EXISTS lca_truncate_sequences;

/* ============================================================
   12) DIVERS (queues/cron) – optionnel
   ============================================================ */
-- TRUNCATE TABLE `cron_schedule`;
-- TRUNCATE TABLE `queue_message`;
-- TRUNCATE TABLE `queue_message_status`;
-- TRUNCATE TABLE `queue_lock`;
-- TRUNCATE TABLE `bulk`;
-- TRUNCATE TABLE `asynchronous_operations`;

-- (Ne pas réactiver les FK ici; la réactivation se fait en fin de script.)

/* ============================================================
   13) MODULES TIERS (Amasty, Blueskytechco, BSS, Taxes)
   ============================================================ */
-- Truncate des tables listées (ne touche pas au CMS)
TRUNCATE TABLE `adminactionlog`;
TRUNCATE TABLE `amasty_ampromo_items_storage`;
TRUNCATE TABLE `amasty_ampromo_rule`;
TRUNCATE TABLE `amasty_amrolepermissions_rule`;
TRUNCATE TABLE `amasty_amrules_purchase_history_index`;
TRUNCATE TABLE `amasty_amrules_purchase_history_index_cl`;
TRUNCATE TABLE `amasty_amrules_rule`;
TRUNCATE TABLE `amasty_amrules_usage_counter`;
TRUNCATE TABLE `amasty_amrules_usage_limit`;
TRUNCATE TABLE `amasty_audit_active_sessions`;
TRUNCATE TABLE `amasty_audit_login_attempts`;
TRUNCATE TABLE `amasty_audit_log_details`;
TRUNCATE TABLE `amasty_audit_log_entry`;
TRUNCATE TABLE `amasty_audit_visit_details`;
TRUNCATE TABLE `amasty_audit_visit_entry`;
TRUNCATE TABLE `amasty_banners_lite_banner_data`;
TRUNCATE TABLE `amasty_banners_lite_rule`;
TRUNCATE TABLE `amasty_banners_lite_rule_sku`;
TRUNCATE TABLE `amasty_banner_rule`;
TRUNCATE TABLE `amasty_banner_rule_products`;
TRUNCATE TABLE `amasty_conditions_quote`;
TRUNCATE TABLE `amasty_file`;
TRUNCATE TABLE `amasty_file_report`;
TRUNCATE TABLE `amasty_file_store`;
TRUNCATE TABLE `amasty_file_store_category_product`;
TRUNCATE TABLE `amasty_fpc_activity`;
TRUNCATE TABLE `amasty_fpc_context_debug`;
TRUNCATE TABLE `amasty_fpc_flushes_log`;
TRUNCATE TABLE `amasty_fpc_log`;
TRUNCATE TABLE `amasty_fpc_queue_page`;
TRUNCATE TABLE `amasty_fpc_reports`;

TRUNCATE TABLE `amasty_label_catalog_parts`;
TRUNCATE TABLE `amasty_label_cl`;
TRUNCATE TABLE `amasty_label_customer_group`;
TRUNCATE TABLE `amasty_label_entity`;
TRUNCATE TABLE `amasty_label_index`;
TRUNCATE TABLE `amasty_label_main_cl`;
TRUNCATE TABLE `amasty_label_store`;
TRUNCATE TABLE `amasty_label_tooltip`;
TRUNCATE TABLE `amasty_merch_dynamic_category_product_cl`;
TRUNCATE TABLE `amasty_merch_dynamic_category_products`;
TRUNCATE TABLE `amasty_merch_dynamic_category_products_replica`;
TRUNCATE TABLE `amasty_merch_dynamic_category_products_temporary`;
TRUNCATE TABLE `amasty_merch_product_dynamic_category_cl`;
TRUNCATE TABLE `amasty_merch_product_dynamic_category_msi_cl`;
TRUNCATE TABLE `amasty_merchandiser_product_index_eav`;
TRUNCATE TABLE `amasty_merchandiser_product_index_eav_replica`;
TRUNCATE TABLE `amasty_merchandiser_product_index_eav_tmp`;
TRUNCATE TABLE `amasty_merchandiser_visible_product`;
TRUNCATE TABLE `amasty_merchandiser_visible_product_replica`;
TRUNCATE TABLE `amasty_merchandiser_visible_product_tmp`;
TRUNCATE TABLE `amasty_method_label`;
TRUNCATE TABLE `amasty_ogrid_attribute`;
TRUNCATE TABLE `amasty_ogrid_attribute_index`;
TRUNCATE TABLE `amasty_ogrid_attribute_index_cl`;
TRUNCATE TABLE `amasty_order_attribute_eav_attribute`;
TRUNCATE TABLE `amasty_order_attribute_eav_attribute_customer_group`;
TRUNCATE TABLE `amasty_order_attribute_eav_attribute_store`;
TRUNCATE TABLE `amasty_order_attribute_entity`;
TRUNCATE TABLE `amasty_order_attribute_entity_datetime`;
TRUNCATE TABLE `amasty_order_attribute_entity_decimal`;
TRUNCATE TABLE `amasty_order_attribute_entity_increment`;
TRUNCATE TABLE `amasty_order_attribute_entity_int`;
TRUNCATE TABLE `amasty_order_attribute_entity_text`;
TRUNCATE TABLE `amasty_order_attribute_entity_varchar`;
TRUNCATE TABLE `amasty_order_attribute_grid_flat`;
TRUNCATE TABLE `amasty_order_attribute_grid_flat_replica`;
TRUNCATE TABLE `amasty_order_attribute_product_cl`;
TRUNCATE TABLE `amasty_order_attribute_product_index`;
TRUNCATE TABLE `amasty_order_attribute_product_index_replica`;
TRUNCATE TABLE `amasty_order_attribute_relation`;
TRUNCATE TABLE `amasty_order_attribute_relation_details`;
TRUNCATE TABLE `amasty_order_attribute_shipping_methods`;
TRUNCATE TABLE `amasty_order_attribute_tooltip`;
TRUNCATE TABLE `amasty_order_attribute_values_cl`;
TRUNCATE TABLE `amasty_perm_dealer_customer`;
TRUNCATE TABLE `amasty_perm_dealer_order`;
TRUNCATE TABLE `amasty_perm_dealer_order_assign_history`;
TRUNCATE TABLE `amasty_pgrid_qty_sold`;
TRUNCATE TABLE `amasty_pgrid_qty_sold_cl`;
TRUNCATE TABLE `amasty_product_order_attribute_cl`;
TRUNCATE TABLE `amasty_quickorder_item`;
TRUNCATE TABLE `amasty_quickorder_list`;
TRUNCATE TABLE `amasty_quote`;
TRUNCATE TABLE `amasty_rma_guest_create_request`;
TRUNCATE TABLE `amasty_rma_history`;
TRUNCATE TABLE `amasty_rma_item_condition`;
TRUNCATE TABLE `amasty_rma_item_condition_store`;
TRUNCATE TABLE `amasty_rma_message`;
TRUNCATE TABLE `amasty_rma_message_file`;
TRUNCATE TABLE `amasty_rma_reason`;
TRUNCATE TABLE `amasty_rma_reason_store`;
TRUNCATE TABLE `amasty_rma_request`;
TRUNCATE TABLE `amasty_rma_request_item`;
TRUNCATE TABLE `amasty_rma_resolution`;
TRUNCATE TABLE `amasty_rma_resolution_store`;
TRUNCATE TABLE `amasty_rma_return_rules`;
TRUNCATE TABLE `amasty_rma_return_rules_customer_groups`;
TRUNCATE TABLE `amasty_rma_return_rules_resolutions`;
TRUNCATE TABLE `amasty_rma_return_rules_websites`;
TRUNCATE TABLE `amasty_rma_status`;
TRUNCATE TABLE `amasty_rma_status_store`;
TRUNCATE TABLE `amasty_rma_tracking`;
TRUNCATE TABLE `amasty_segments_customer_cl`;
TRUNCATE TABLE `amasty_segments_event`;
TRUNCATE TABLE `amasty_segments_index`;
TRUNCATE TABLE `amasty_segments_segment`;
TRUNCATE TABLE `amasty_segments_website`;
TRUNCATE TABLE `amasty_shiprestriction_attribute`;
TRUNCATE TABLE `amasty_shiprestriction_rule`;
TRUNCATE TABLE `blueskytechco_product_index_most_viewed`;
TRUNCATE TABLE `blueskytechco_product_widget_advanced_bestseller`;
TRUNCATE TABLE `blueskytechco_product_widget_advanced_most_viewed`;
TRUNCATE TABLE `blueskytechco_product_widget_advanced_review_rate`;
TRUNCATE TABLE `bss_applied_customers`;
TRUNCATE TABLE `bss_price_rules`;
TRUNCATE TABLE `bss_product_price`;
TRUNCATE TABLE `bss_attribute_dependent`;
TRUNCATE TABLE `bss_customer_rule_attribute_map`;
TRUNCATE TABLE `bss_price_rule_attributes`;
TRUNCATE TABLE `mst_seo_filter_attribute_config`;
TRUNCATE TABLE `mst_seo_filter_rewrite`;
TRUNCATE TABLE `tax_order_aggregated_created`;
TRUNCATE TABLE `tax_order_aggregated_updated`;

-- Reset AUTO_INCREMENT uniquement si la table possède une colonne auto_increment
SET @db = DATABASE();
-- Liste des tables à vérifier pour AUTO_INCREMENT
-- Pour chaque table, on teste et on ALTER si applicable
SET @tables_ai = 'adminactionlog,amasty_ampromo_items_storage,amasty_ampromo_rule,amasty_amrolepermissions_rule,amasty_amrules_purchase_history_index,amasty_amrules_purchase_history_index_cl,amasty_amrules_rule,amasty_amrules_usage_counter,amasty_amrules_usage_limit,amasty_audit_active_sessions,amasty_audit_login_attempts,amasty_audit_log_details,amasty_audit_log_entry,amasty_audit_visit_details,amasty_audit_visit_entry,amasty_banners_lite_banner_data,amasty_banners_lite_rule,amasty_banners_lite_rule_sku,amasty_banner_rule,amasty_banner_rule_products,amasty_conditions_quote,amasty_file,amasty_file_report,amasty_file_store,amasty_file_store_category_product,amasty_fpc_activity,amasty_fpc_context_debug,amasty_fpc_flushes_log,amasty_fpc_log,amasty_fpc_queue_page,amasty_fpc_reports,amasty_groupcat_customer_cl,amasty_groupcat_product_cl,amasty_groupcat_rule,amasty_groupcat_rule_category,amasty_groupcat_rule_cl,amasty_groupcat_rule_customer_group,amasty_groupcat_rule_product,amasty_groupcat_rule_product_replica,amasty_groupcat_rule_store,amasty_label_catalog_parts,amasty_label_cl,amasty_label_customer_group,amasty_label_entity,amasty_label_index,amasty_label_main_cl,amasty_label_store,amasty_label_tooltip,amasty_merch_dynamic_category_product_cl,amasty_merch_dynamic_category_products,amasty_merch_dynamic_category_products_replica,amasty_merch_dynamic_category_products_temporary,amasty_merch_product_dynamic_category_cl,amasty_merch_product_dynamic_category_msi_cl,amasty_merchandiser_product_index_eav,amasty_merchandiser_product_index_eav_replica,amasty_merchandiser_product_index_eav_tmp,amasty_merchandiser_visible_product,amasty_merchandiser_visible_product_replica,amasty_merchandiser_visible_product_tmp,amasty_method_label,amasty_ogrid_attribute,amasty_ogrid_attribute_index,amasty_ogrid_attribute_index_cl,amasty_order_attribute_eav_attribute,amasty_order_attribute_eav_attribute_customer_group,amasty_order_attribute_eav_attribute_store,amasty_order_attribute_entity,amasty_order_attribute_entity_datetime,amasty_order_attribute_entity_decimal,amasty_order_attribute_entity_increment,amasty_order_attribute_entity_int,amasty_order_attribute_entity_text,amasty_order_attribute_entity_varchar,amasty_order_attribute_grid_flat,amasty_order_attribute_grid_flat_replica,amasty_order_attribute_product_cl,amasty_order_attribute_product_index,amasty_order_attribute_product_index_replica,amasty_order_attribute_relation,amasty_order_attribute_relation_details,amasty_order_attribute_shipping_methods,amasty_order_attribute_tooltip,amasty_order_attribute_values_cl,amasty_perm_dealer,amasty_perm_dealer_customer,amasty_perm_dealer_order,amasty_perm_dealer_order_assign_history,amasty_pgrid_qty_sold,amasty_pgrid_qty_sold_cl,amasty_product_order_attribute_cl,amasty_quickorder_item,amasty_quickorder_list,amasty_quote,amasty_rma_guest_create_request,amasty_rma_history,amasty_rma_item_condition,amasty_rma_item_condition_store,amasty_rma_message,amasty_rma_message_file,amasty_rma_reason,amasty_rma_reason_store,amasty_rma_request,amasty_rma_request_item,amasty_rma_resolution,amasty_rma_resolution_store,amasty_rma_return_rules,amasty_rma_return_rules_customer_groups,amasty_rma_return_rules_resolutions,amasty_rma_return_rules_websites,amasty_rma_status,amasty_rma_status_store,amasty_rma_tracking,amasty_segments_customer_cl,amasty_segments_event,amasty_segments_index,amasty_segments_segment,amasty_segments_website,amasty_shipping_area,amasty_shiprestriction_attribute,amasty_shiprestriction_rule,amasty_table_method,amasty_table_rate,amasty_table_rate_replica,amasty_table_rate_sources,blueskytechco_product_index_most_viewed,blueskytechco_product_widget_advanced_bestseller,blueskytechco_product_widget_advanced_most_viewed,blueskytechco_product_widget_advanced_review_rate,bss_applied_customers,bss_price_rules,bss_product_price,bss_attribute_dependent,bss_customer_rule_attribute_map,bss_price_rule_attributes,tax_calculation,tax_calculation_rate,tax_calculation_rate_title,tax_calculation_rule,tax_order_aggregated_created,tax_order_aggregated_updated';

-- Boucle sur la liste (en SQL pur via REPLACE + information_schema)
-- Remarque: MySQL ne supporte pas une boucle simple en SQL pur sans procédure; on déclenche via une requête qui génère ALTER pour les tables possédant un AI
DELIMITER //
DROP PROCEDURE IF EXISTS lca_reset_autoincrement//
CREATE PROCEDURE lca_reset_autoincrement()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE t VARCHAR(255);
  DECLARE cur CURSOR FOR
    SELECT t.table_name
    FROM information_schema.tables t
    JOIN information_schema.columns c
      ON c.table_schema = t.table_schema AND c.table_name = t.table_name AND c.EXTRA LIKE '%auto_increment%'
    WHERE t.table_schema=@db
      AND FIND_IN_SET(t.table_name, REPLACE(@tables_ai, ' ', '')) > 0;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur;
  loop_ai: LOOP
    FETCH cur INTO t;
    IF done THEN
      LEAVE loop_ai;
    END IF;
    SET @sql_ai = CONCAT('ALTER TABLE `', t, '` AUTO_INCREMENT=1');
    PREPARE stmt_ai FROM @sql_ai;
    EXECUTE stmt_ai;
    DEALLOCATE PREPARE stmt_ai;
  END LOOP;
  CLOSE cur;
END//
DELIMITER ;

CALL lca_reset_autoincrement();
DROP PROCEDURE IF EXISTS lca_reset_autoincrement;

/* ============================================================
   14) CATALOG EAV – supprimer les attributs dont le code commence par 'nc_'
       (UNIQUEMENT produits et catégories; ne touche pas aux attributs client)
   ============================================================ */
DROP TEMPORARY TABLE IF EXISTS tmp_nc_catalog_attrs;
CREATE TEMPORARY TABLE tmp_nc_catalog_attrs
SELECT a.attribute_id
FROM eav_attribute a
WHERE a.attribute_code LIKE 'nc\_%'
  AND a.entity_type_id IN (
    SELECT entity_type_id
    FROM eav_entity_type
    WHERE entity_type_code IN ('catalog_product','catalog_category')
  );

-- Supprimer les valeurs EAV produits et catégories pour ces attributs
DELETE v FROM catalog_product_entity_int       v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_decimal   v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_varchar   v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_text      v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_datetime  v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;

DELETE v FROM catalog_category_entity_int       v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_decimal   v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_varchar   v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_text      v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_datetime  v JOIN tmp_nc_catalog_attrs t ON t.attribute_id = v.attribute_id;

-- Supprimer dépendances d'attributs (catalog + eav générique) sans toucher aux tables customer*
DELETE ce FROM catalog_eav_attribute           ce JOIN tmp_nc_catalog_attrs t ON t.attribute_id = ce.attribute_id;
DELETE al FROM eav_attribute_label             al JOIN tmp_nc_catalog_attrs t ON t.attribute_id = al.attribute_id;
DELETE aov FROM eav_attribute_option_value      aov
  JOIN eav_attribute_option ao ON ao.option_id = aov.option_id
  JOIN tmp_nc_catalog_attrs t ON t.attribute_id = ao.attribute_id;
DELETE aos FROM eav_attribute_option_swatch     aos
  JOIN eav_attribute_option ao ON ao.option_id = aos.option_id
  JOIN tmp_nc_catalog_attrs t ON t.attribute_id = ao.attribute_id;
DELETE ao  FROM eav_attribute_option            ao JOIN tmp_nc_catalog_attrs t ON t.attribute_id = ao.attribute_id;
DELETE eea FROM eav_entity_attribute            eea JOIN tmp_nc_catalog_attrs t ON t.attribute_id = eea.attribute_id;

-- Enfin, supprimer les attributs eux-mêmes
DELETE a FROM eav_attribute a
JOIN tmp_nc_catalog_attrs t ON t.attribute_id = a.attribute_id;

-- Reset AUTO_INCREMENT sur les tables concernées (si applicable)
ALTER TABLE `eav_attribute` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_option` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_label` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_option_swatch` AUTO_INCREMENT=1;
ALTER TABLE `eav_entity_attribute` AUTO_INCREMENT=1;

/* ============================================================
   15) Correctifs AUTO_INCREMENT complémentaires
   ============================================================ */
-- Catégories: après DELETE (conservation des IDs 1 et 2), on remet l'AI à 1
ALTER TABLE `catalog_category_entity` AUTO_INCREMENT=1;
-- Tables EAV catégories (valeurs) nettoyées via DELETE
ALTER TABLE `catalog_category_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `catalog_category_entity_text` AUTO_INCREMENT=1;

/* ============================================================
   16) Nettoyage des options/valeurs de l'attribut catalogue 'manufacturer'
       (ne supprime PAS l'attribut lui-même)
   ============================================================ */
-- Récupère l'ID de l'attribut 'manufacturer' pour les produits
SET @manu_attr_id = (
  SELECT a.attribute_id
  FROM eav_attribute a
  JOIN eav_entity_type t ON t.entity_type_id = a.entity_type_id
  WHERE a.attribute_code = 'manufacturer'
    AND t.entity_type_code = 'catalog_product'
  LIMIT 1
);

-- Supprimer les valeurs produits pointant vers des options 'manufacturer'
DELETE FROM catalog_product_entity_int WHERE attribute_id = @manu_attr_id;

-- Supprimer d'abord les valeurs d'options, puis les swatches, puis les options
DELETE aov
FROM eav_attribute_option_value aov
JOIN eav_attribute_option ao ON ao.option_id = aov.option_id
WHERE ao.attribute_id = @manu_attr_id;

DELETE aos
FROM eav_attribute_option_swatch aos
JOIN eav_attribute_option ao ON ao.option_id = aos.option_id
WHERE ao.attribute_id = @manu_attr_id;

DELETE FROM eav_attribute_option WHERE attribute_id = @manu_attr_id;

-- Reset AUTO_INCREMENT si applicable
ALTER TABLE `eav_attribute_option` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_option_swatch` AUTO_INCREMENT=1;

/* ============================================================
   17) Nettoyage des options/valeurs pour les attributs EAV avec code commençant par 'nc_'
       (UNIQUEMENT catalogue produit & catégorie; ne supprime PAS les attributs)
       Note: Cette section est redondante avec la section 14 mais conservée pour compatibilité
       ============================================================ */
DROP TEMPORARY TABLE IF EXISTS tmp_attrs_nc;
CREATE TEMPORARY TABLE tmp_attrs_nc
SELECT a.attribute_id, a.entity_type_id
FROM eav_attribute a
JOIN eav_entity_type t ON t.entity_type_id = a.entity_type_id
WHERE a.attribute_code LIKE 'nc\_%'
  AND t.entity_type_code IN ('catalog_product','catalog_category');

-- Supprimer les valeurs EAV produits et catégories
DELETE v FROM catalog_product_entity_int       v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_decimal   v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_varchar   v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_text      v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_product_entity_datetime  v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;

DELETE v FROM catalog_category_entity_int       v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_decimal   v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_varchar   v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_text      v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;
DELETE v FROM catalog_category_entity_datetime  v JOIN tmp_attrs_nc t ON t.attribute_id = v.attribute_id;

-- Supprimer d'abord valeurs d'options, puis swatches, puis options
DELETE aov FROM eav_attribute_option_value aov
JOIN eav_attribute_option ao ON ao.option_id = aov.option_id
JOIN tmp_attrs_nc t ON t.attribute_id = ao.attribute_id;

DELETE aos FROM eav_attribute_option_swatch aos
JOIN eav_attribute_option ao ON ao.option_id = aos.option_id
JOIN tmp_attrs_nc t ON t.attribute_id = ao.attribute_id;

DELETE ao FROM eav_attribute_option ao
JOIN tmp_attrs_nc t ON t.attribute_id = ao.attribute_id;

-- Reset AUTO_INCREMENT si applicable
ALTER TABLE `eav_attribute_option` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_option_swatch` AUTO_INCREMENT=1;
ALTER TABLE `eav_attribute_option_value` AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;

-- Post-exécution (en CLI):
-- bin/magento indexer:reindex
-- bin/magento cache:clean && bin/magento cache:flush
