  toggleOutOfStock: protectedProcedure
    .input(z.object({
      id: z.number(),
    }))
    .mutation(async ({ input }): Promise<AdminToggleOutOfStockResult> => {
      const { id } = input;

      const updatedProduct = await toggleProductOutOfStockInDb(id)


      if (!updatedProduct) {
        throw new ApiError('Product not found', 404)
      }

      await scheduleStoreInitialization()

      return {
        product: updatedProduct,
        message: `Product marked as ${updatedProduct.isOutOfStock ? 'out of stock' : 'in stock'}`,
      }
    }),




    export async function toggleProductOutOfStock(id: number): Promise<AdminProduct | null> {
  const product = await db.query.productInfo.findFirst({
    where: eq(productInfo.id, id),
  })

  if (!product) {
    return null
  }

  const [updatedProduct] = await db
    .update(productInfo)
    .set({
      isOutOfStock: !product.isOutOfStock,
    })
    .where(eq(productInfo.id, id))
    .returning()

  if (!updatedProduct) {
    return null
  }

  return mapProduct(updatedProduct)
}