package queries

// GetWholesaleCampaignVariantStockByProducts:
// Returns per-product variant keys (ColorCode/ItemDim1Code/ItemDim2Code-as-ItemDim3Code) and available stock qty.
// We aggregate across warehouses/stores; semantics align with product-stock-query's "Kullanilabilir_Envanter".
// In this Nebim setup, the second user-visible/key variant maps to ItemDim2Code, while PG stores it in mmitem_dim.val3.
const GetWholesaleCampaignVariantStockByProducts = `
DECLARE @Codes NVARCHAR(MAX) = @p1;

;WITH INP AS (
  -- SQL Server 2008 compatibility: string_split() does not exist.
  -- Split CSV via XML nodes().
  SELECT LTRIM(RTRIM(X.C.value('.', 'NVARCHAR(50)'))) AS ItemCode
  FROM (
    SELECT CAST('<i>' + REPLACE(REPLACE(@Codes, '&', '&amp;'), ',', '</i><i>') + '</i>' AS XML) AS XmlData
  ) D
  CROSS APPLY D.XmlData.nodes('/i') AS X(C)
  WHERE LTRIM(RTRIM(X.C.value('.', 'NVARCHAR(50)'))) <> ''
),
VARIANT_MASTER AS (
  SELECT
    V.ItemCode,
    LTRIM(RTRIM(ISNULL(V.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(V.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(V.ItemDim1Code,'')))) AS ItemDim1Code
  FROM prItemVariant V
  JOIN INP ON INP.ItemCode = V.ItemCode
  WHERE V.ItemTypeCode = 1
    AND LEN(V.ItemCode) = 13
  GROUP BY
    V.ItemCode, V.ColorCode, V.ItemDim2Code
),
VARIANT_STOCK AS (
  SELECT
    S.ItemCode,
    LTRIM(RTRIM(ISNULL(S.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(S.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(S.ItemDim1Code,'')))) AS ItemDim1Code
  FROM trStock S
  JOIN INP ON INP.ItemCode = S.ItemCode
  WHERE S.ItemTypeCode = 1
    AND LEN(S.ItemCode) = 13
  GROUP BY
    S.ItemCode, S.ColorCode, S.ItemDim2Code
),
VARIANT AS (
  SELECT
    X.ItemCode,
    X.ColorCode,
    X.ItemDim3Code,
    MAX(X.ItemDim1Code) AS ItemDim1Code
  FROM (
    SELECT ItemCode, ColorCode, ItemDim3Code, ItemDim1Code FROM VARIANT_MASTER
    UNION ALL
    SELECT ItemCode, ColorCode, ItemDim3Code, ItemDim1Code FROM VARIANT_STOCK
  ) X
  GROUP BY X.ItemCode, X.ColorCode, X.ItemDim3Code
),
STOCK AS (
  SELECT
    S.ItemCode,
    LTRIM(RTRIM(ISNULL(S.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(S.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(S.ItemDim1Code,'')))) AS ItemDim1Code,
    SUM(S.In_Qty1 - S.Out_Qty1)              AS InventoryQty1
  FROM trStock S
  JOIN INP ON INP.ItemCode = S.ItemCode
  WHERE S.ItemTypeCode = 1
    AND LEN(S.ItemCode) = 13
  GROUP BY
    S.ItemCode, S.ColorCode, S.ItemDim2Code
),
PICK AS (
  SELECT
    P.ItemCode,
    LTRIM(RTRIM(ISNULL(P.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(P.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(P.ItemDim1Code,'')))) AS ItemDim1Code,
    SUM(P.Qty1)                              AS PickingQty1
  FROM PickingStates P
  JOIN INP ON INP.ItemCode = P.ItemCode
  WHERE P.ItemTypeCode = 1
    AND LEN(P.ItemCode) = 13
  GROUP BY
    P.ItemCode, P.ColorCode, P.ItemDim2Code
),
RESERVE AS (
  SELECT
    R.ItemCode,
    LTRIM(RTRIM(ISNULL(R.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(R.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(R.ItemDim1Code,'')))) AS ItemDim1Code,
    SUM(R.Qty1)                              AS ReserveQty1
  FROM ReserveStates R
  JOIN INP ON INP.ItemCode = R.ItemCode
  WHERE R.ItemTypeCode = 1
    AND LEN(R.ItemCode) = 13
  GROUP BY
    R.ItemCode, R.ColorCode, R.ItemDim2Code
),
DISP AS (
  SELECT
    D.ItemCode,
    LTRIM(RTRIM(ISNULL(D.ColorCode,'')))     AS ColorCode,
    LTRIM(RTRIM(ISNULL(D.ItemDim2Code,'')))  AS ItemDim3Code,
    MAX(LTRIM(RTRIM(ISNULL(D.ItemDim1Code,'')))) AS ItemDim1Code,
    SUM(D.Qty1)                              AS DispOrderQty1
  FROM DispOrderStates D
  JOIN INP ON INP.ItemCode = D.ItemCode
  WHERE D.ItemTypeCode = 1
    AND LEN(D.ItemCode) = 13
  GROUP BY
    D.ItemCode, D.ColorCode, D.ItemDim2Code
)
SELECT
  V.ItemCode     AS ItemCode,
  V.ColorCode    AS ColorCode,
  V.ItemDim1Code AS ItemDim1Code,
  V.ItemDim3Code AS ItemDim3Code,
  CAST(ROUND(
    ISNULL(S.InventoryQty1,0)
    - ISNULL(PK.PickingQty1,0)
    - ISNULL(RS.ReserveQty1,0)
    - ISNULL(DP.DispOrderQty1,0),
    2
  ) AS FLOAT) AS StockQty
FROM VARIANT V
LEFT JOIN STOCK S
  ON S.ItemCode=V.ItemCode AND S.ColorCode=V.ColorCode AND S.ItemDim3Code=V.ItemDim3Code
LEFT JOIN PICK PK
  ON PK.ItemCode=V.ItemCode AND PK.ColorCode=V.ColorCode AND PK.ItemDim3Code=V.ItemDim3Code
LEFT JOIN RESERVE RS
  ON RS.ItemCode=V.ItemCode AND RS.ColorCode=V.ColorCode AND RS.ItemDim3Code=V.ItemDim3Code
LEFT JOIN DISP DP
  ON DP.ItemCode=V.ItemCode AND DP.ColorCode=V.ColorCode AND DP.ItemDim3Code=V.ItemDim3Code
ORDER BY V.ItemCode, V.ColorCode, V.ItemDim3Code;
`
