import { sendJson } from '../http.js';
import { getCategoryCount } from '../queries/category-count.js';
import { getMaxOrderIdCount } from '../queries/max-order-id.js';
import { getCompositeProductCount } from '../queries/composite-product-count.js';
import { getCustomerGroupCount } from '../queries/customer-groups.js';
import { getDeletedEntityCount } from '../queries/deleted-entity-count.js';
import { getProductCount } from '../queries/product-count.js';

export const method = 'GET';
export const path = '/v1/init';

export async function handle(_req, res, { url }) {
  const productCursor = Number(url.searchParams.get('lastChangedProduct')) || 0;
  const categoryCursor = Number(url.searchParams.get('lastChangedCategory')) || 0;
  const customerGroupCursor = Number(url.searchParams.get('lastChangedCustomerGroup')) || 0;
  const compositeProductCursor = Number(url.searchParams.get('lastChangedCompositeProduct')) || 0;
  const deletedEntityCursor = Number(url.searchParams.get('lastChangedDeletedEntity')) || 0;

  const [productCount, categoryCount, customerGroupCount, compositeProductCount, deletedEntityCount, maxOrderIdCount] = await Promise.all([
    getProductCount({ cursor: productCursor }),
    getCategoryCount({ cursor: categoryCursor }),
    getCustomerGroupCount({ cursor: customerGroupCursor }),
    getCompositeProductCount({ cursor: compositeProductCursor }),
    getDeletedEntityCount({ cursor: deletedEntityCursor }),
    getMaxOrderIdCount(),
  ]);

  return sendJson(res, 200, {
    version: '1.10.12.0',
    product_count: String(productCount),
    category_count: String(categoryCount),
    customer_count: '0',
    customerGroup_count: String(customerGroupCount),
    compositeProduct_count: String(compositeProductCount),
    configurationGroup_count: '0',
    configurationItem_count: '0',
    deletedEntity_count: String(deletedEntityCount),
    max_orderId_count: String(maxOrderIdCount),
  });
}
