-- Скрипт для очистки данных склада и входящих поставок фулфилмента
-- Очищает только Supply и SupplyOrder, НЕ удаляет сам кабинет

-- Показываем что будет удалено
SELECT 
  'Данные для очистки в кабинете фулфилмента:' as info,
  (SELECT COUNT(*) FROM supplies WHERE "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')) as supplies_count,
  (SELECT COUNT(*) FROM supplies WHERE type = 'FULFILLMENT_CONSUMABLES') as fulfillment_supplies_count,
  (SELECT COUNT(*) FROM supply_orders WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')) as supply_orders_as_fulfillment_count,
  (SELECT COUNT(*) FROM supply_orders WHERE "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')) as supply_orders_created_by_fulfillment_count,
  (SELECT COUNT(*) FROM supply_order_items WHERE "supplyOrderId" IN (
    SELECT id FROM supply_orders 
    WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
       OR "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
  )) as supply_order_items_count;

-- ОСТОРОЖНО! Раскомментируйте следующие строки для выполнения очистки:

/*
-- 1. Удаляем элементы заказов поставок (supply_order_items)
DELETE FROM supply_order_items 
WHERE "supplyOrderId" IN (
  SELECT id FROM supply_orders 
  WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
     OR "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
);

-- 2. Удаляем заказы поставок (SupplyOrder)
DELETE FROM supply_orders 
WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
   OR "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT');

-- 3. Удаляем расходники со склада (Supply)
DELETE FROM supplies 
WHERE "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
   OR type = 'FULFILLMENT_CONSUMABLES';

-- Показываем результат после очистки
SELECT 
  'Результат очистки:' as info,
  (SELECT COUNT(*) FROM supplies WHERE "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')) as remaining_supplies,
  (SELECT COUNT(*) FROM supply_orders WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT') OR "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')) as remaining_supply_orders;
*/