CREATE TABLE `CouponTypeHasTag` (
  `couponTypeId` INT(10) UNSIGNED NOT NULL,
  `tagId`        INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY (`couponTypeId`, `tagId`)
)
  COLLATE = 'utf8_general_ci';

ALTER TABLE `CouponTypeHasTag`
  ADD CONSTRAINT `FK1_CouponTypeHasTag_couponTypeId_Id` FOREIGN KEY (`couponTypeId`) REFERENCES `CouponType` (`id`)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION,
  ADD CONSTRAINT `FK2_CouponTypeHasTag_tagId_Id` FOREIGN KEY (`tagId`) REFERENCES `Tag` (`id`)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION,
  ADD INDEX `FK1_CouponTypeHasTag_couponTypeId_Id_idx` (`couponTypeId`),
  ADD INDEX `FK2_CouponTypeHasTag_tagId_Id_idx` (`couponTypeId`);

ALTER TABLE `CouponType`
  ADD COLUMN `hasFreeShipping` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0'
  AFTER `validForCartTotal`,
  ADD COLUMN `hasFreeReturn` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0'
  AFTER `hasFreeShipping`;


ALTER TABLE `Coupon`
  DROP COLUMN `tagId`,
  DROP COLUMN `amountType`,
  DROP INDEX `fk_Coupon_Tag1_idx`,
  DROP FOREIGN KEY `fk_Coupon_Tag1`;

UPDATE Sidebar
SET pageId = 50
WHERE pageId = 51;

INSERT INTO Page (slug, url, icon, permission)
VALUES ('coupon_add_smart', '/blueseal/marketing/coupon/smart', 'fa-gift', 'allShops');
INSERT INTO PageTranslation (pageId, langId, title) VALUES (?, 1, 'Coupon Smart');
INSERT INTO Sidebar (pageId, sidebarGroupId, pageOrder) VALUES (?, 10, 160);

ALTER TABLE `Coupon`
  ADD COLUMN `couponEventId` INT(10) UNSIGNED NULL
  AFTER `valid`;
ALTER TABLE `Coupon`
  ADD CONSTRAINT `fk_Coupon_CouponEvent` FOREIGN KEY (`couponEventId`) REFERENCES `CouponEvent` (`id`)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION;

UPDATE PageTranslation SET title = 'Coupon Multiplo', description = null, keywords = null WHERE pageId = 53 AND langId = 1;
UPDATE PageTranslation SET title = 'Modifica coupon multiplo', description = null, keywords = null WHERE pageId = 54 AND langId = 1;
UPDATE PageTranslation SET title = 'Aggiungi coupon multiplo', description = null, keywords = null WHERE pageId = 55 AND langId = 1;