-- =======================================================
-- USER TABLE
-- =======================================================

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

-- =======================================================
-- MENU ITEM TABLE
-- =======================================================

CREATE TABLE menu(
    id SERIAL PRIMARY KEY,
    name VARCHAR(250) NOT NULL,
    description TEXT,
    price NUMERIC NOT NULL CHECK (price > 0),
    category VARCHAR(50)
);

-- =======================================================
-- ORDER TABLE
-- =======================================================

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    created_at TIMESTAMP NOT NULL,
    total_price NUMERIC NOT NULL,

    FOREIGN KEY (user_id) REFERENCES users(id)
);

-- =======================================================
-- ORDER DETAIL TABLE
-- =======================================================

CREATE TABLE order_details(
    id SERIAL PRIMARY KEY,
    order_id INT NOT NULL,
    menu_id INT NOT NULL,
    quantity NUMERIC NOT NULL CHECK (quantity > 0),
    item_price NUMERIC NOT NULL CHECK (item_price > 0),

    FOREIGN KEY (order_id) REFERENCES orders(id),
    FOREIGN KEY (menu_id) REFERENCES menu(id)
);

-- =======================================================
-- INITIAL MENU DATA
-- =======================================================

insert into menu (name, description, price, category) values ('Margherita Pizza', 'Classic pizza with tomato sauce, mozzarella, and basil.', 12.99, 'Pizza');
insert into menu (name, description, price, category) values ('Pepperoni Pizza', null, 14.49, 'Pizza');
insert into menu (name, description, price, category) values ('Cheeseburger', 'Beef patty with cheddar cheese, lettuce, and tomato.', 10.99, 'Burger');
insert into menu (name, description, price, category) values ('Chicken Burger', null, 11.49, 'Burger');
insert into menu (name, description, price, category) values ('Caesar Salad', 'Romaine lettuce, croutons, parmesan, and Caesar dressing.', 8.99, 'Salad');
insert into menu (name, description, price, category) values ('Greek Salad', null, 9.49, 'Salad');
insert into menu (name, description, price, category) values ('French Fries', 'Crispy golden potato fries.', 4.99, 'Side');
insert into menu (name, description, price, category) values ('Onion Rings', null, 5.49, 'Side');
insert into menu (name, description, price, category) values ('Espresso', null, 2.99, 'Drink');
insert into menu (name, description, price, category) values ('Cappuccino', 'Espresso with steamed milk and milk foam.', 4.49, 'Drink');
insert into menu (name, description, price, category) values ('Chocolate Cake', null, 6.99, 'Dessert');
insert into menu (name, description, price, category) values ('Cheesecake', 'Creamy cheesecake served with berry sauce.', 7.49, 'Dessert');

-- =======================================================
-- VERIFICATION QUERIES
-- =======================================================

SELECT * FROM users;
SELECT * FROM menu;
SELECT * FROM orders;
SELECT * FROM order_details;
