-- Webshop System Grundschema

CREATE TABLE IF NOT EXISTS ws_shop (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    domain VARCHAR(255) NOT NULL,
    domain_ssl VARCHAR(255) NOT NULL,
    physical_uri VARCHAR(255) NOT NULL,
    theme VARCHAR(64) NOT NULL DEFAULT 'classic'
);

CREATE TABLE IF NOT EXISTS ws_user (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    firstname VARCHAR(100),
    lastname VARCHAR(100),
    is_admin TINYINT(1) NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS ws_language (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(64) NOT NULL,
    iso_code VARCHAR(8) NOT NULL,
    locale VARCHAR(16) NOT NULL
);

CREATE TABLE IF NOT EXISTS ws_country (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(64) NOT NULL,
    iso_code VARCHAR(8) NOT NULL
);

CREATE TABLE IF NOT EXISTS ws_configuration (
    id INT AUTO_INCREMENT PRIMARY KEY,
    config_key VARCHAR(128) NOT NULL UNIQUE,
    config_value TEXT
);

CREATE TABLE IF NOT EXISTS ws_category (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    parent_id INT DEFAULT NULL,
    FOREIGN KEY (parent_id) REFERENCES ws_category(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS ws_product (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    price DECIMAL(15,2) NOT NULL,
    stock INT NOT NULL DEFAULT 0,
    category_id INT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES ws_category(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS ws_cart (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES ws_user(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS ws_cart_product (
    cart_id INT,
    product_id INT,
    quantity INT NOT NULL DEFAULT 1,
    PRIMARY KEY (cart_id, product_id),
    FOREIGN KEY (cart_id) REFERENCES ws_cart(id) ON DELETE CASCADE,
    FOREIGN KEY (product_id) REFERENCES ws_product(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS ws_order (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    total DECIMAL(15,2) NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'pending',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES ws_user(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS ws_order_product (
    order_id INT,
    product_id INT,
    quantity INT NOT NULL DEFAULT 1,
    price DECIMAL(15,2) NOT NULL,
    PRIMARY KEY (order_id, product_id),
    FOREIGN KEY (order_id) REFERENCES ws_order(id) ON DELETE CASCADE,
    FOREIGN KEY (product_id) REFERENCES ws_product(id) ON DELETE CASCADE
); 