CREATE TABLE users(
                      id BIGSERIAL PRIMARY KEY,
                      username VARCHAR(20) NOT NULL UNIQUE,
                      password_hash VARCHAR(256) NOT NULL,
                      email VARCHAR(50)
);

CREATE TABLE menu_items(
                           id BIGSERIAL PRIMARY KEY,
                           name VARCHAR(50) NOT NULL,
                           description  VARCHAR(100),
                           price DECIMAL NOT NULL CHECK (price > 0),
                           category VARCHAR(50)
);

CREATE TABLE customer_orders(
                                id BIGSERIAL PRIMARY KEY,
                                user_id BIGINT NOT NULL,
                                time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
                                total_price DECIMAL(10 , 2),
                                CONSTRAINT fk_user_id
                                    FOREIGN KEY (user_id)
                                        REFERENCES users(id)
);


CREATE TABLE order_details (
                               id BIGSERIAL PRIMARY KEY,
                               order_id BIGINT NOT NULL,
                               menu_item_id BIGINT NOT NULL,
                               quantity INTEGER NOT NULL CHECK (quantity > 0),
                               unit_price DECIMAL(10 , 2) NOT NULL,

                               CONSTRAINT fk_order
                                   FOREIGN KEY (order_id)
                                       REFERENCES customer_orders(id) ,

                               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 pizza with tomato and mozzarella', 12.99, 'Pizza'),
                                                                  ('Pepperoni Pizza', 'Pizza with pepperoni and cheese', 14.99, 'Pizza'),
                                                                  ('Cheeseburger', 'Beef burger with cheddar cheese', 10.99, 'Burger'),
                                                                  ('Spaghetti Carbonara', 'Pasta with eggs, cheese, and pancetta', 13.99, 'Pasta'),
                                                                  ('Coca-Cola', 'Classic carbonated soft drink', 3.99, 'Drink'),
                                                                  ('Sparkling Water', 'Natural sparkling mineral water', 2.99, 'Drink');

