CREATE TABLE users (
                       id SERIAL PRIMARY KEY,
                       username VARCHAR(50) UNIQUE NOT NULL,
                       password_hash VARCHAR(255) NOT NULL,
                       email VARCHAR(100)
);

CREATE TABLE menu_items (
                            id SERIAL PRIMARY KEY,
                            name VARCHAR(100) NOT NULL,
                            description TEXT,
                            price NUMERIC(10, 2) NOT NULL CHECK (price > 0),
                            category VARCHAR(50)
);

CREATE TABLE orders (
                        id SERIAL PRIMARY KEY,
                        user_id INT NOT NULL,
                        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                        total_price NUMERIC(10, 2) NOT NULL,
                        CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE order_details (
                               id SERIAL PRIMARY KEY,
                               order_id INT NOT NULL,
                               menu_item_id INT NOT NULL,
                               quantity INT NOT NULL CHECK (quantity > 0),
                               unit_price NUMERIC(10, 2) NOT NULL,
                               CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
                               CONSTRAINT fk_menu_item FOREIGN KEY (menu_item_id) REFERENCES menu_items(id)
);

INSERT INTO menu_items (name, description, price, category) VALUES
                                                                ('Margherita Pizza', 'Classic cheese and tomato', 10.00, 'Pizza'),
                                                                ('Cheeseburger', 'Beef patty with cheese', 8.00, 'Burger'),
                                                                ('Spaghetti Carbonara', 'Pasta with creamy sauce and bacon', 12.00, 'Pasta');