-- Flagi kontroli pobierania URL z Merchant Center

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'products'
      AND COLUMN_NAME = 'merchant_url_not_found'
  ),
  'DO 1',
  'ALTER TABLE `products` ADD COLUMN `merchant_url_not_found` TINYINT(1) NOT NULL DEFAULT 0 AFTER `product_url`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'products'
      AND COLUMN_NAME = 'merchant_url_last_check'
  ),
  'DO 1',
  'ALTER TABLE `products` ADD COLUMN `merchant_url_last_check` DATETIME NULL DEFAULT NULL AFTER `merchant_url_not_found`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Normalizacja: puste/sztuczne wartosci URL traktujemy jako brak URL.
UPDATE `products`
SET `product_url` = NULL
WHERE TRIM( COALESCE( `product_url`, '' ) ) = ''
   OR LOWER( TRIM( `product_url` ) ) IN ( '0', '-', 'null' );

