DROP DATABASE IF EXISTS TechStore;
CREATE DATABASE TechStore;

USE TechStore;

CREATE TABLE Users(
    UserID INTEGER PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(32),
    Email VARCHAR(256),
    Password VARCHAR(255)
);

CREATE TABLE Categories(
    CategoryID INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(64) NOT NULL
);

CREATE TABLE Products(
    ProductID INTEGER PRIMARY KEY AUTO_INCREMENT,
    ProductName VARCHAR(104),
    Description TEXT,
    CategoryID INT,
    Price DECIMAL(10,2),
    ImagePath VARCHAR(255),
    StockQuantity INT DEFAULT 0,
    FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);

CREATE TABLE Orders(
    OrderID INT PRIMARY KEY AUTO_INCREMENT,
    UserID INT,
    OrderDate DATETIME DEFAULT CURRENT_TIMESTAMP,
    Total DECIMAL(10,2),
    Status VARCHAR(20) DEFAULT 'pending',
    FOREIGN KEY (UserID) REFERENCES Users(UserID)
);

CREATE TABLE OrderItems(
    OrderItemID INT PRIMARY KEY AUTO_INCREMENT,
    OrderID INT,
    ProductID INT,
    Quantity INT,
    Price DECIMAL(10,2),
    FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
    FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

-- INSERIMENTO DATI
INSERT INTO Categories(Name) VALUES
('Elettronica'),('Networking'),('Telefonia');

INSERT INTO Products(ProductName,Description,CategoryID,Price,ImagePath,StockQuantity) VALUES
('Yealink T31P',NULL,3,'51.21','example/t31p.jpg',104);