CREATE DATABASE IF NOT EXISTS market;
USE market;
CREATE TABLE IF NOT EXISTS users(
    rowId INT PRIMARY KEY AUTO INCREMENT,
    userId VARCHAR(36) UNIQUE NOT NULL,
    userName VARCHAR(64) UNIQUE NOT NULL,
    passwordHash VARCHAR(64) NOT NULL,
    email VARCHAR(64) UNIQUE NOT NULL,
    balance FLOAT NOT NULL DEFAULT 0,
    public_key VARCHAR(128) NOT NULL
);
CREATE TABLE IF NOT EXISTS buyOrders(
    rowId INT PRIMARY KEY AUTO INCREMENT,
    buyOrderId VARCHAR(36) UNIQUE NOT NULL,
    pageId INT NOT NULL,
    price FLOAT NOT NULL,
    volume INT NOT NULL,
    fee FLOAT NOT NULL DEFAULT 0,
    FOREIGN KEY (userId) REFERENCES users(userId)
);
CREATE TABLE IF NOT EXISTS sellOrders(
    rowId INT PRIMARY KEY AUTO INCREMENT,
    sellOrderId VARCHAR(36) UNIQUE NOT NULL,
    price FLOAT NOT NULL,
    fee FLOAT NOT NULL,
    FOREIGN KEY (userId) REFERENCES users(userId)
);
CREATE TABLE IF NOT EXISTS sellOrdersItems(
    rowId INT PRIMARY KEY AUTO INCREMENT,
    cardId VARCHAR(36) NOT NULL,
    FOREIGN KEY (sellOrderId) REFERENCES sellOrders(sellOrderId) NOT NULL,
);
CREATE TABLE IF NOT EXISTS transactions(
    rowId INT PRIMARY KEY AUTO INCREMENT,
    transactionId VARCHAR(36) UNIQUE NOT NULL,
    isPending BOOLEAN NOT NULL DEFAULT true,
    isAbandoned BOOLEAN NOT NULL DEFAULT false,
    FOREIGN KEY (buyOrderId) REFERENCES buyOrders(buyOrderId) NOT NULL,
    FOREIGN KEY (sellOrderId) REFERENCES sellOrders(sellOrderId) NOT NULL
);
