CREATE TABLE IF NOT EXISTS `product_manufacturer` ( 
`id_product_manufacturer` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`active` tinyint(1),
PRIMARY KEY (`id_product_manufacturer`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `product_attr` ( 
`id_product_attr` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`type` int(11),
`options` varchar(255) CHARACTER SET utf8,
`active` tinyint(1),
PRIMARY KEY (`id_product_attr`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `product_group` ( 
`id_product_group` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`view_order` int(11),
`id_resource_type_page` int(11),
PRIMARY KEY (`id_product_group`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `product` ( 
`id_product` int(11) NOT NULL AUTO_INCREMENT,
`id_product_group` int(11),
`article` VARCHAR(255) NOT NULL, 
`photo` varchar(255) CHARACTER SET utf8,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`short_desc` varchar(255) CHARACTER SET utf8,
`description` text,
`old_price` float(10,2),
`price` float(10,2),
`wholesale_price` float(10,2),
`count` int(11),
`id_product_manufacturer` int(11),
`id_resource_type_page` int(11),
PRIMARY KEY (`id_product`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `product` ADD INDEX(`id_product_group`);
ALTER TABLE `product` ADD INDEX(`id_product_manufacturer`);
ALTER TABLE `product` ADD FOREIGN KEY (`id_product_group`) REFERENCES `product_group`(`id_product_group`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `product` ADD FOREIGN KEY (`id_product_manufacturer`) REFERENCES `product_manufacturer`(`id_product_manufacturer`) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS `product_icon` ( 
`id_product_icon` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`icon` varchar(255) CHARACTER SET utf8 NOT NULL,
`active` tinyint(1),
PRIMARY KEY (`id_product_icon`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `product_group_linker` (
  `id_product_group_linker` int(11) NOT NULL AUTO_INCREMENT,
  `id_product_group` int(11) NOT NULL,
  `id_product_attr` int(11) NOT NULL,
PRIMARY KEY (`id_product_group_linker`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `product_group_linker` ADD INDEX(`id_product_group`);
ALTER TABLE `product_group_linker` ADD INDEX(`id_product_attr`);
ALTER TABLE `product_group_linker` ADD FOREIGN KEY (`id_product_group`) REFERENCES `product_group`(`id_product_group`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `product_group_linker` ADD FOREIGN KEY (`id_product_attr`) REFERENCES `product_attr`(`id_product_attr`) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS `product_attr_value` (
  `id_product_attr_value` int(11) NOT NULL AUTO_INCREMENT,
  `id_product_attr` int(11) NOT NULL,
  `id_product` int(11) NOT NULL,
  `value` text,
PRIMARY KEY (`id_product_attr_value`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `product_attr_value` ADD INDEX(`id_product_attr`);
ALTER TABLE `product_attr_value` ADD INDEX(`id_product`);
ALTER TABLE `product_attr_value` ADD FOREIGN KEY (`id_product_attr`) REFERENCES `product_attr`(`id_product_attr`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `product_attr_value` ADD FOREIGN KEY (`id_product`) REFERENCES `product`(`id_product`) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS `product_icon_linker` (
  `id_product_icon_linker` int(11) NOT NULL AUTO_INCREMENT,
  `id_product` int(11) NOT NULL,
  `id_product_icon` int(11) NOT NULL,
PRIMARY KEY (`id_product_icon_linker`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `product_icon_linker` ADD INDEX(`id_product`);
ALTER TABLE `product_icon_linker` ADD INDEX(`id_product_icon`);
ALTER TABLE `product_icon_linker` ADD FOREIGN KEY (`id_product`) REFERENCES `product`(`id_product`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `product_icon_linker` ADD FOREIGN KEY (`id_product_icon`) REFERENCES `product_icon`(`id_product_icon`) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS `product_related_linker` (
  `id_product_related_linker` int(11) NOT NULL AUTO_INCREMENT,
  `id_product` int(11) NOT NULL,
  `id_related_product` int(11) NOT NULL,
PRIMARY KEY (`id_product_related_linker`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `product_related_linker` ADD INDEX(`id_product`);
ALTER TABLE `product_related_linker` ADD INDEX(`id_related_product`);
ALTER TABLE `product_related_linker` ADD FOREIGN KEY (`id_product`) REFERENCES `product`(`id_product`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `product_related_linker` ADD FOREIGN KEY (`id_related_product`) REFERENCES `product`(`id_product`) ON DELETE CASCADE ON UPDATE CASCADE;

-- Insert data
INSERT INTO `admin_menu` (`id_admin_menu`, `name`, `page`, `image`, `parent_id`, `view_order`, `acl_resource`, `acl_action`, `active`) VALUES (NULL, 'Products', NULL, 'fa-shopping-cart', '0', '100', 'product', '', '1');
