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

-- Account Section

CREATE TABLE IF NOT EXISTS `Account` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `UserName` varchar(60) NOT NULL,
  `Email` varchar(255) NOT NULL,
  `EmailVerified` boolean DEFAULT 0,
  `PasswordHash` char(60) DEFAULT NULL,
  `FailedPasswordLock` boolean DEFAULT 0,
  `PasswordAttempts` int(11) DEFAULT NULL,
  `CurrentPasswordAttempts` int(11) DEFAULT NULL,
  `Role` varchar(45) DEFAULT NULL,
  `EmailToken` varchar(45) DEFAULT NULL,
  `DataServer` varchar(200) DEFAULT NULL,
  `ServerRegion` varchar(10) DEFAULT NULL,
  UNIQUE(`Email`),
  UNIQUE(`UserName`),
  PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;

-- Resume Section

CREATE TABLE IF NOT EXISTS `Resume` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `AccountID` int NOT NULL,
  `Name` varchar(100) NOT NULL,
  `Field` varchar(100) DEFAULT NULL,
  `Email` varchar(255) NOT NULL,
  `PhoneNumber` varchar(20) NOT NULL,
  `PostalCode` varchar(20) NOT NULL,
  `Country` char(2) NOT NULL,
  `StateOrRegion` varchar(100) NOT NULL,
  `City` varchar(100) NOT NULL,
  `IsActive` boolean DEFAULT 0,
  PRIMARY KEY (`ID`),
  FOREIGN KEY (`AccountID`) REFERENCES `Account`(`ID`) ON DELETE CASCADE
) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeExperience` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `JobTitle` varchar(150) NOT NULL,
    `Company` varchar(100) NOT NULL,
    `PostalCode` varchar(20) NOT NULL,
    `Country` char(2) NOT NULL,
    `StateOrRegion` varchar(100) NOT NULL,
    `City` varchar(100) NOT NULL,
    `DateStarted` date NOT NULL,
    `StillEmployed` boolean DEFAULT 0,
    `DateEnded` date DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

    CREATE TABLE IF NOT EXISTS `ResumeExperienceBullet` (
      `ID` int NOT NULL AUTO_INCREMENT,
      `ResumeID` int NOT NULL,
      `ResumeExperienceID` int NOT NULL,
      `JobFunction` text NOT NULL,
      PRIMARY KEY (`ID`),
      FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE,
      FOREIGN KEY (`ResumeExperienceID`) REFERENCES `ResumeExperience`(`ID`) ON DELETE CASCADE
    ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeMillitary` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `Country` char(2) NOT NULL,
    `Rank` varchar(50) NOT NULL,
    `DateStarted` date NOT NULL,
    `StillServing` boolean DEFAULT 0,
    `DateEnded` date DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

    CREATE TABLE IF NOT EXISTS `ResumeMillitaryBullet` (
      `ID` int NOT NULL AUTO_INCREMENT,
      `ResumeID` int NOT NULL,
      `ResumeMillitaryID` int NOT NULL,
      `Achevement` varchar(100) NOT NULL,
      `Description` text DEFAULT NULL,
      PRIMARY KEY (`ID`),
      FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE,
      FOREIGN KEY (`ResumeMillitaryID`) REFERENCES `ResumeMillitary`(`ID`) ON DELETE CASCADE
    ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeEducation` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `DegreeType` varchar(150) NOT NULL,
    `DegreeField` varchar(150) NOT NULL,
    `School` varchar(150) NOT NULL,
    `PostalCode` varchar(20) NOT NULL,
    `Country` char(2) NOT NULL,
    `StateOrRegion` varchar(100) NOT NULL,
    `City` varchar(100) NOT NULL,
    `DateStarted` date NOT NULL,
    `StillStudying` boolean DEFAULT 0,
    `DateEnded` date DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeSkill` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `Name` varchar(150) NOT NULL,
    `Description` text DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeLanguage` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `Language` varchar(150) NOT NULL,
    `Proficiency` text DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeCertification` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `Name` varchar(150) NOT NULL,
    `VerificationURL` varchar(200) DEFAULT NULL,
    `Description` text DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

  CREATE TABLE IF NOT EXISTS `ResumeProject` (
    `ID` int NOT NULL AUTO_INCREMENT,
    `ResumeID` int NOT NULL,
    `Name` varchar(150) NOT NULL,
    `URL` varchar(200) DEFAULT NULL,
    `Description` text DEFAULT NULL,
    PRIMARY KEY (`ID`),
    FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE
  ) AUTO_INCREMENT=1;

-- Company Section

CREATE TABLE IF NOT EXISTS `Company` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `Name` varchar(100) DEFAULT NULL,
  `Email` varchar(255) DEFAULT NULL,
  `EmailVerified` boolean DEFAULT 0,
  `WebsiteURL` VARCHAR(255) DEFAULT NULL,
  `LogoURL` VARCHAR(2048) DEFAULT NULL,
  `Phone` VARCHAR(20) DEFAULT NULL,
  `PostalCode` varchar(20) NOT NULL,
  `Country` CHAR(2) NOT NULL,
  `StateOrRegion` varchar(100) NOT NULL,
  `City` VARCHAR(100),
  `Description` TEXT,
  PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `JobListing` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `CompanyID` int NOT NULL,
  `Title` varchar(100) NOT NULL,
  `PostalCode` varchar(20) NOT NULL,
  `Country` char(2) NOT NULL,
  `StateOrRegion` varchar(100) NOT NULL,
  `City` varchar(100) NOT NULL,
  `SalaryMin` int NOT NULL,
  `SalaryMax` int NOT NULL,
  `JobType` varchar(20) NOT NULL,
  `Remote` boolean DEFAULT 0,
  `Description` text NOT NULL,
  PRIMARY KEY (`ID`),
  FOREIGN KEY (`CompanyID`) REFERENCES `Company`(`ID`) ON DELETE CASCADE
) AUTO_INCREMENT=1;

-- Default Account

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',
  ''
);