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

-- Resume Section

CREATE TABLE IF NOT EXISTS `Resume` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `AccountID` int NOT NULL,
  `Title` varchar(100) 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`)
) 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 `ResumeMilitary` (
    `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 `ResumeMilitaryBullet` (
      `ID` int NOT NULL AUTO_INCREMENT,
      `ResumeID` int NOT NULL,
      `ResumeMilitaryID` int NOT NULL,
      `Achievement` varchar(100) NOT NULL,
      `Description` text DEFAULT NULL,
      PRIMARY KEY (`ID`),
      FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE,
      FOREIGN KEY (`ResumeMilitaryID`) REFERENCES `ResumeMilitary`(`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,
  `EmailToken` char(36) DEFAULT NULL,
  `WebsiteURL` varchar(255) DEFAULT NULL,
  `Logo` mediumblob DEFAULT NULL,
  `JobsClosedSuccessful` int DEFAULT 0,
  `JobsAutoClosed` int DEFAULT 0,
  `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 `Employee` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `AccountID` int NOT NULL,
  `AccountName` varchar(60) NOT NULL,
  `AccountEmail` varchar(255) NOT NULL,
  `CompanyID` int NOT NULL,
  PRIMARY KEY (`ID`),
  FOREIGN KEY (`CompanyID`) REFERENCES `Company`(`ID`) ON DELETE CASCADE
) 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,
  `CreatedTime`  datetime DEFAULT NULL,
  `ModifiedTime` datetime DEFAULT NULL,
  `IsDeleted` boolean DEFAULT 0,
  PRIMARY KEY (`ID`),
  FOREIGN KEY (`CompanyID`) REFERENCES `Company`(`ID`) ON DELETE CASCADE
) AUTO_INCREMENT=1;

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

-- Postal Codes Section

CREATE TABLE IF NOT EXISTS `PostalCodes` (
  `CountryCode` char(2),
  `PostalCode` varchar(20),
  `City` varchar(180),
  `State` varchar(100),
  `StateCode` varchar(20),
  `County` varchar(100),
  `CountyCode` varchar(20),
  `Community` varchar(100),
  `CommunityCode` varchar(20),
  `Latitude` float,
  `Longitude` float,
  `Accuracy` varchar(2)
);

LOAD DATA INFILE '/var/lib/mysql-files/postalcodes.csv'
INTO TABLE PostalCodes
FIELDS TERMINATED BY '\t'
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

CREATE INDEX idx_postal_country ON PostalCodes (City, PostalCode, CountryCode, Latitude, Longitude);

-- Application Section

CREATE TABLE IF NOT EXISTS `JobApplication` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `AccountID` int NOT NULL,
  `ResumeID` int NOT NULL,
  `JobListingID` int NOT NULL,
  `ResponseEmail` varchar(255) DEFAULT NULL,
  `DateApplied` datetime DEFAULT NULL,
  `ResponseStatus` varchar(50) NOT NULL DEFAULT 'Pending',
  `HasBeenViewed` boolean DEFAULT 0,
  `Rating` int DEFAULT NULL,
  `Notes` text DEFAULT NULL,
  PRIMARY KEY (`ID`),
  FOREIGN KEY (`ResumeID`) REFERENCES `Resume`(`ID`) ON DELETE CASCADE,
  FOREIGN KEY (`JobListingID`) REFERENCES `JobListing`(`ID`) ON DELETE CASCADE
) AUTO_INCREMENT=1;