-- Restaurant Database Management System

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

-- =======================================================
-- MENU ITEM TABLE
-- =======================================================
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)
);

-- =======================================================
-- ORDER TABLE
-- =======================================================
CREATE TABLE orders (
                        id          SERIAL PRIMARY KEY,
                        user_id     INT            NOT NULL REFERENCES users(id) ON DELETE CASCADE,
                        created_at  TIMESTAMP      NOT NULL DEFAULT NOW(),
                        total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)
);

-- =======================================================
-- ORDER DETAIL TABLE
-- =======================================================
CREATE TABLE order_details (
                               id           SERIAL PRIMARY KEY,
                               order_id     INT            NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
                               menu_item_id INT            NOT NULL REFERENCES menu_items(id),
                               quantity     INT            NOT NULL CHECK (quantity > 0),
                               unit_price   NUMERIC(10, 2) NOT NULL CHECK (unit_price > 0)
);

-- =======================================================
-- INITIAL MENU DATA
-- =======================================================
INSERT INTO menu_items (name, description, price, category) VALUES
                                                                ('Margherita Pizza',  'Classic tomato sauce and mozzarella',   10.00, 'Pizza'),
                                                                ('BBQ Burger',        'Juicy beef patty with BBQ sauce',         8.50, 'Burger'),
                                                                ('Spaghetti Carbonara', 'Creamy pasta with pancetta and egg',   12.00, 'Pasta'),
                                                                ('Tiramisu',          'Italian coffee dessert',                   5.50, 'Dessert'),
                                                                ('Cola',              'Ice-cold soft drink',                      2.50, 'Drink');