package queries

const GetProductAttributes = `
;WITH TypeDesc AS (
	SELECT
		t.ItemTypeCode,
		t.AttributeTypeCode,
		ISNULL(t.AttributeTypeDescription, CAST(t.AttributeTypeCode AS NVARCHAR(30))) AS AttributeTypeDescription
	FROM dbo.cdItemAttributeTypeDesc AS t WITH(NOLOCK)
	WHERE t.ItemTypeCode = @p1
	  AND t.LangCode = 'TR'
),
Attr AS (
	SELECT
		a.ItemTypeCode,
		a.AttributeTypeCode,
		ISNULL(a.AttributeCode, '') AS AttributeCode,
		ISNULL(d.AttributeDescription, ISNULL(a.AttributeCode, '')) AS AttributeDescription
	FROM dbo.cdItemAttribute AS a WITH(NOLOCK)
	LEFT JOIN dbo.cdItemAttributeDesc AS d WITH(NOLOCK)
		ON d.ItemTypeCode = a.ItemTypeCode
		AND d.AttributeTypeCode = a.AttributeTypeCode
		AND d.AttributeCode = a.AttributeCode
		AND d.LangCode = 'TR'
	WHERE a.ItemTypeCode = @p1
	  AND ISNULL(a.IsBlocked, 0) = 0
),
SELECT
	a.ItemTypeCode,
	a.AttributeTypeCode,
	ISNULL(NULLIF(td.AttributeTypeDescription, ''), CAST(a.AttributeTypeCode AS NVARCHAR(30))) AS AttributeTypeDescription,
	a.AttributeCode,
	a.AttributeDescription
FROM Attr a
LEFT JOIN TypeDesc td
	ON td.ItemTypeCode = a.ItemTypeCode
	AND td.AttributeTypeCode = a.AttributeTypeCode
ORDER BY
	a.AttributeTypeCode,
	CASE WHEN a.AttributeCode = '-' THEN 0 ELSE 1 END,
	a.AttributeCode;
`
