<?php
if (!defined('ABSPATH'))
  exit;

use AC_SYNC\Includes\Classes\Api\Action_Wear_Api_Route;
use AC_SYNC\Includes\Classes\Api\Action_Wear_API_Response as ApiResponse;
use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;

$route = new Action_Wear_Api_Route("/nuke_all_products");
$route->setMethods("POST");

$route->setCallback(function () {
  global $wpdb;

  $response = new ApiResponse();

  // Safety: only allow on localhost
  $host = parse_url(site_url(), PHP_URL_HOST);
  if ($host !== 'localhost' && $host !== '127.0.0.1') {
    $response->fail("This action is only available on localhost");
    return $response->getResponse();
  }

  @set_time_limit(600);
  @ini_set('memory_limit', '512M');

  $prefix = $wpdb->prefix;

  // Tranche 1: Transaction — delete all product-related data
  $wpdb->query("START TRANSACTION");

  try {
    // Term relationships for products
    $wpdb->query(
      "DELETE tr FROM {$prefix}term_relationships tr
       INNER JOIN {$prefix}posts p ON tr.object_id = p.ID
       WHERE p.post_type IN ('product', 'product_variation')"
    );

    // Post meta for products
    $wpdb->query(
      "DELETE pm FROM {$prefix}postmeta pm
       INNER JOIN {$prefix}posts p ON pm.post_id = p.ID
       WHERE p.post_type IN ('product', 'product_variation')"
    );

    // Comment meta for product comments
    $wpdb->query(
      "DELETE cpm FROM {$prefix}commentmeta cpm
       INNER JOIN {$prefix}comments c ON cpm.comment_id = c.comment_ID
       INNER JOIN {$prefix}posts p ON c.comment_post_ID = p.ID
       WHERE p.post_type IN ('product', 'product_variation')"
    );

    // Comments on products
    $wpdb->query(
      "DELETE c FROM {$prefix}comments c
       INNER JOIN {$prefix}posts p ON c.comment_post_ID = p.ID
       WHERE p.post_type IN ('product', 'product_variation')"
    );

    // Products and variations
    $wpdb->query(
      "DELETE FROM {$prefix}posts
       WHERE post_type IN ('product', 'product_variation')"
    );

    $wpdb->query("COMMIT");
  } catch (\Throwable $e) {
    $wpdb->query("ROLLBACK");
    Log::write("Nuke products: Transaction failed - " . $e->getMessage(), Log::ERROR, Log::CONTEXT_GENERAL);
    $response->fail("Transaction failed: " . $e->getMessage());
    return $response->getResponse();
  }

  // Tranche 2: Cleanup orphaned terms (outside transaction)
  $wpdb->query(
    "DELETE tt FROM {$prefix}term_taxonomy tt
     LEFT JOIN {$prefix}term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
     WHERE tr.term_taxonomy_id IS NULL"
  );

  $wpdb->query(
    "DELETE t FROM {$prefix}terms t
     LEFT JOIN {$prefix}term_taxonomy tt ON t.term_id = tt.term_id
     WHERE tt.term_id IS NULL"
  );

  // Reset actionwear_products wc_created flag
  $wpdb->query("UPDATE {$prefix}actionwear_products SET wc_created = 0");

  Log::write("Nuke products: All WooCommerce products deleted", Log::WARNING, Log::CONTEXT_GENERAL);

  $response->success([
    "success" => true,
    "message" => "All WooCommerce products deleted"
  ]);
  return $response->getResponse();
});

$route->registerRoute();
