CREATE DATABASE IF NOT EXISTS `mistox`;
USE `mistox`;

CREATE TABLE IF NOT EXISTS `Account` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `UserName` varchar(60) DEFAULT NULL,
  `Email` varchar(60) DEFAULT NULL,
  `EmailVerified` tinyint(4) DEFAULT NULL,
  `PasswordHash` varchar(100) DEFAULT NULL,
  `FailedPasswordLock` tinyint(4) DEFAULT NULL,
  `PasswordAttempts` int(11) DEFAULT NULL,
  `CurrentPasswordAttempts` int(11) DEFAULT NULL,
  `Role` varchar(45) DEFAULT NULL,
  `EmailToken` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `Product` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(45) DEFAULT NULL,
  `Description` text,
  `Cost` int(11) DEFAULT NULL,
  `URL` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `ProductImage` (
  `ImageID` int(11) NOT NULL AUTO_INCREMENT,
  `ProductID` int(11) NOT NULL,
  `Image` MEDIUMBLOB,
  `Name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ImageID`,`ProductID`)
) AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `ProductInventory` (
  `AccountID` int(11) NOT NULL,
  `ProductID` int(11) NOT NULL,
  `Key` varchar(45) NOT NULL,
  `Value` text DEFAULT NULL,
  PRIMARY KEY (`AccountID`,`ProductID`,`Key`)
);

CREATE TABLE IF NOT EXISTS `Cart` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `AccountID` int(11) DEFAULT NULL,
  `ProductID` int(11) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `AccountID` (`AccountID`),
  KEY `ProductID` (`ProductID`),
  CONSTRAINT `Cart_ibfk_1` FOREIGN KEY (`AccountID`) REFERENCES `Account` (`ID`),
  CONSTRAINT `Cart_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `Product` (`ID`)
) AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `Receipt` (
  `AccountID` int(11) NOT NULL,
  `ProductID` int(11) NOT NULL,
  `ReceiptID` varchar(45) NOT NULL,
  `LineItem` int(11) NOT NULL,
  `Time` datetime DEFAULT NULL,
  `TaxAmount` int(11) DEFAULT NULL,
  `TotalCost` int(11) DEFAULT NULL,
  PRIMARY KEY (`AccountID`,`ProductID`,`ReceiptID`)
);

INSERT INTO Account (
  ID,
  UserName,
  Email,
  EmailVerified,
  PasswordHash,
  FailedPasswordLock,
  PasswordAttempts,
  CurrentPasswordAttempts,
  Role,
  EmailToken
) VALUES (
  1,
  'admin',
  'admin@mistox.com',
  1,
  '$2a$11$0UeWLLqTXe3FG161QVuI0OQJ9rulspUpMG581DI6KSzDXBbFKd00S',
  1,
  1,
  5,
  0,
  'Admin',
  ''
);