CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(20) NOT NULL UNIQUE,
    password VARCHAR(20) NOT NULL,
    email VARCHAR(100) UNIQUE
);


CREATE TABLE menu_items(
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) 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 INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMP NOT NULL,
    total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)

);


CREATE TABLE order_detail(
    id SERIAL PRIMARY KEY,
    order_id INTEGER NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
    menu_item_id INTEGER NOT NULL REFERENCES menu_items(id),
    quantity INTEGER NOT NULL CHECK (quantity > 0),
    price NUMERIC(10, 2) NOT NULL CHECK (price > 0)
);


INSERT INTO menu_items (name, description, price, category)
VALUES
    ('Margherita Classica', 'Tomato sauce, fresh mozzarella, and basil on a crispy thin crust', 16.00, 'Pizza'),
    ('Smoked BBQ Bacon Burger', 'Angus beef with cheddar, bacon, onion rings, and BBQ sauce on a brioche bun.', 14.50, 'Burger'),
    ('Truffle Mushroom Fettuccine', 'Creamy fettuccine with mushrooms, Parmesan, and a touch of truffle oil.', 18.00, 'Pasta'),
    ('Cola', 'Cold soft drink', 2.50, 'Drink');

--
SELECT * FROM users;
SELECT * FROM menu_items;
SELECT * FROM orders;
SELECT * FROM order_detail;