import sql from 'mssql';
import { getActiveShopId } from '../shop.js';

export const CATEGORY_TREE_CTE = `
WITH CategoryTree AS (
    SELECT kKategorie FROM dbo.tKategorie WHERE kKategorie = @rootCategoryId
    UNION ALL
    SELECT t.kKategorie
    FROM dbo.tKategorie t
    INNER JOIN CategoryTree ct ON t.kOberKategorie = ct.kKategorie
)`;

export function getRootCategoryId() {
  return Number(process.env.ROOT_CATEGORY_ID) || 1;
}

export function categoryTreeRequest(pool, rootCategoryId = getRootCategoryId()) {
  return pool
    .request()
    .input('rootCategoryId', sql.Int, rootCategoryId)
    .input('kShop', sql.Int, getActiveShopId());
}
