const { PrismaClient } = require('@prisma/client')

const prisma = new PrismaClient()

async function clearFulfillmentData() {
  console.log('🧹 Очищаем данные склада и поставок фулфилмента...')

  try {
    // Удаляем элементы заказов поставок
    await prisma.$executeRaw`
      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')
      )
    `

    // Удаляем заказы поставок
    await prisma.$executeRaw`
      DELETE FROM supply_orders 
      WHERE "fulfillmentCenterId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
         OR "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
    `

    // Удаляем расходники
    await prisma.$executeRaw`
      DELETE FROM supplies 
      WHERE "organizationId" IN (SELECT id FROM organizations WHERE type = 'FULFILLMENT')
         OR type = 'FULFILLMENT_CONSUMABLES'
    `

    console.log('✅ Данные склада и поставок фулфилмента очищены!')
    
  } catch (error) {
    console.error('❌ Ошибка:', error)
  } finally {
    await prisma.$disconnect()
  }
}

clearFulfillmentData()