-- SQL schema generated from Sequelize models in sims/backend-project
-- Target: MySQL

SET FOREIGN_KEY_CHECKS = 0;

-- 1) Spare Parts
DROP TABLE IF EXISTS `Stock_In`;
DROP TABLE IF EXISTS `Stock_Out`;
DROP TABLE IF EXISTS `Spare_Parts`;
DROP TABLE IF EXISTS `Users`;

SET FOREIGN_KEY_CHECKS = 1;

CREATE TABLE `Spare_Parts` (
  `Name` VARCHAR(255) NOT NULL,
  `Category` VARCHAR(255) NOT NULL,
  `Quantity` INT NOT NULL DEFAULT 0,
  `UnitPrice` DECIMAL(10,2) NOT NULL,
  `TotalPrice` DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (`Name`)
) ENGINE=InnoDB;

CREATE TABLE `Stock_In` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `StockInQuantity` INT NOT NULL,
  `StockInDate` DATETIME NOT NULL,
  `SparePartName` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `idx_stockin_sparepart` (`SparePartName`),
  CONSTRAINT `fk_stockin_sparepart`
    FOREIGN KEY (`SparePartName`) REFERENCES `Spare_Parts` (`Name`)
    ON UPDATE CASCADE
    ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE `Stock_Out` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `StockOutQuantity` INT NOT NULL,
  `StockOutUnitPrice` DECIMAL(10,2) NOT NULL,
  `StockOutTotalPrice` DECIMAL(10,2) NOT NULL,
  `StockOutDate` DATETIME NOT NULL,
  `SparePartName` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `idx_stockout_sparepart` (`SparePartName`),
  CONSTRAINT `fk_stockout_sparepart`
    FOREIGN KEY (`SparePartName`) REFERENCES `Spare_Parts` (`Name`)
    ON UPDATE CASCADE
    ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE `Users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_users_username` (`username`),
  UNIQUE KEY `uq_users_email` (`email`)
) ENGINE=InnoDB;

