CREATE TABLE `vn`.`wagonType` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL UNIQUE,
  `divisible` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `vn`.`wagonTypeColor` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL UNIQUE,
  `rgb` varchar(30) NOT NULL UNIQUE,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `vn`.`wagonTypeTray` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `typeFk` int(11) unsigned,
  `height` int(11) unsigned NOT NULL,
  `colorFk` int(11) unsigned,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`typeFk`,`height`),
  CONSTRAINT `wagonTypeTray_type` FOREIGN KEY (`typeFk`) REFERENCES `wagonType` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `wagonTypeTray_color` FOREIGN KEY (`colorFk`) REFERENCES `wagonTypeColor` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `vn`.`wagonConfig` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `width` int(11) unsigned DEFAULT 1350,
  `height` int(11) unsigned DEFAULT 1900,
  `maxWagonHeight` int(11) unsigned DEFAULT 200,
  `minHeightBetweenTrays` int(11) unsigned DEFAULT 50,
  `maxTrays` int(11) unsigned DEFAULT 6,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3;

CREATE TABLE `vn`.`collectionWagon` (
  `collectionFk` int(11) NOT NULL,
  `wagonFk` int(11) NOT NULL,
  `position` int(11) unsigned,
  PRIMARY KEY (`collectionFk`,`position`),
  UNIQUE KEY `collectionWagon_unique` (`collectionFk`,`wagonFk`),
  CONSTRAINT `collectionWagon_collection` FOREIGN KEY (`collectionFk`) REFERENCES `collection` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `collectionWagon_wagon` FOREIGN KEY (`wagonFk`) REFERENCES `wagon` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

CREATE TABLE `vn`.`collectionWagonTicket` (
  `ticketFk` int(11) NOT NULL,
  `wagonFk` int(11) NOT NULL,
  `trayFk` int(11) unsigned NOT NULL,
  `side` SET('L', 'R') NULL,
  PRIMARY KEY (`ticketFk`),
  CONSTRAINT `collectionWagonTicket_ticket` FOREIGN KEY (`ticketFk`) REFERENCES `ticket` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `collectionWagonTicket_wagon` FOREIGN KEY (`wagonFk`) REFERENCES `wagon` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `collectionWagonTicket_tray` FOREIGN KEY (`trayFk`) REFERENCES `wagonTypeTray` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

ALTER TABLE `vn`.`wagon` ADD `typeFk` int(11) unsigned NOT NULL;
ALTER TABLE `vn`.`wagon` ADD `label` int(11) unsigned NOT NULL;
ALTER TABLE `vn`.`wagon` ADD CONSTRAINT `wagon_type` FOREIGN KEY (`typeFk`) REFERENCES `wagonType` (`id`) ON UPDATE CASCADE;

INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
    VALUES
        ('WagonType', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonTypeColor', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonTypeTray', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonConfig', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('CollectionWagon', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('CollectionWagonTicket', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('Wagon', '*', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonType', 'createWagonType', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonType', 'deleteWagonType', '*', 'ALLOW', 'ROLE', 'productionAssi'),
        ('WagonType', 'editWagonType', '*', 'ALLOW', 'ROLE', 'productionAssi');
