package queries

const GetProductAttributes = `
;WITH RequiredTypes AS (
	SELECT
		t.ItemTypeCode,
		t.AttributeTypeCode,
		ISNULL(NULLIF(td.AttributeTypeDescription, ''), CAST(t.AttributeTypeCode AS NVARCHAR(30))) AS AttributeTypeDescription
	FROM dbo.cdItemAttributeType AS t
	LEFT JOIN dbo.cdItemAttributeTypeDesc AS td
		ON td.ItemTypeCode = t.ItemTypeCode
		AND td.AttributeTypeCode = t.AttributeTypeCode
		AND td.LangCode = 'TR'
	WHERE t.ItemTypeCode = @p1
	  AND ISNULL(t.IsBlocked, 0) = 0
	  AND ISNULL(t.IsRequired, 0) = 1
),
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
	LEFT JOIN dbo.cdItemAttributeDesc AS d
		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
	  AND ISNULL(a.AttributeCode, '') <> ''
)
SELECT
	rt.ItemTypeCode,
	rt.AttributeTypeCode,
	rt.AttributeTypeDescription,
	a.AttributeCode,
	a.AttributeDescription
FROM RequiredTypes AS rt
LEFT JOIN Attr AS a
	ON a.ItemTypeCode = rt.ItemTypeCode
	AND a.AttributeTypeCode = rt.AttributeTypeCode
WHERE ISNULL(a.AttributeCode, '') <> ''
ORDER BY
	rt.AttributeTypeCode,
	CASE WHEN a.AttributeCode IN ('-', '.') THEN 0 ELSE 1 END,
	a.AttributeCode;
`

const GetProductItemAttributes = `
SELECT
	a.ItemTypeCode,
	a.AttributeTypeCode,
	ISNULL(a.AttributeCode, '') AS AttributeCode
FROM dbo.prItemAttribute AS a
WHERE a.ItemTypeCode = @p1
  AND ISNULL(LTRIM(RTRIM(a.ItemCode)), '') = ISNULL(LTRIM(RTRIM(@p2)), '')
`
