--------------------------------------------
-- Logistics/orders related tables
--------------------------------------------

-- Our known customers, may need more fields
CREATE TABLE IF NOT EXISTS customers (
  customer_id int PRIMARY KEY,
  customer_name varchar(100),
  customer_address varchar(255)
);

-- Orders from customers.
-- Keep in mind that the delivery_date may be NULL
CREATE TABLE IF NOT EXISTS orders (
  order_id int PRIMARY KEY,
  customer_id int,
  order_datetime datetime DEFAULT NOW,
  preferred_delivery_date DATE, --the table Delivery tells when the delivery is arrived
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

CREATE TABLE IF NOT EXISTS delivery (
    order_id INT,
    truck_id INT,
    delivery_datetime DATETIME,
    PRIMARY KEY (order_id, truck_id),
    FOREIGN KEY (order_id) REFERENCES orders(order_id)
);


CREATE TABLE OrderSpec (
    order_id INT,
    cookie_id INT,
    quantity INT,
    PRIMARY KEY (orderID, cookieID),
    FOREIGN KEY (order_id) REFERENCES Orders(order_id),
    FOREIGN KEY (cookie_id) REFERENCES Cookies(cookie_id)
);

CREATE TABLE IF NOT EXISTS pallet (
    pallet_id INT PRIMARY KEY,
    order_id INT,
    cookie_id INT,
    production_datetime DATETIME,
    is_blocked BOOLEAN,
    pallet_location VARCHAR(255),
    FOREIGN KEY (order_id) REFERENCES Orders(order_id),
    FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
);


--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------

CREATE TABLE IF NOT EXISTS cookies (
    cookie_id INT PRIMARY KEY,
    recipe_id INT,
    cookie_name VARCHAR(255),
    price INT,
    FOREIGN KEY (recipeID) REFERENCES Recipe(recipeID)
);


-- Recipes for all the cookies (essentially a list of cookies)
CREATE TABLE IF NOT EXISTS recipes (
  recipe_id int PRIMARY KEY,
  cookie_id int,
  recipe_name varchar(100) UNIQUE, -- Cookie name
  FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
);

--weak table which descirbies the amout of the ingredient
--needed for a recipe
CREATE TABLE RecipeIngredients (
    ingredient_id INT,
    recipe_id INT,
    quantity FLOAT,
    PRIMARY KEY (ingredientID, recipeID),
    FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id),
    FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id)
);

-- "The company has a raw materials warehouse in which 
--  all ingredients used in their production are stored."

-- Describes ingredients and stock.
-- Each ingredient has 'amount' of 'unit' in stock
CREATE TABLE IF NOT EXISTS ingredients (
  ingredient_id int PRIMARY KEY,
  ingredient_name varchar(100),
  in_stock float,
  unit varchar(50),
  amout_purchase float,
  last_delivery DATETIME
);

-- use this table instead of amount_purchase and last_delivery attributes
-- in ingrdients table if the assigment want us to have a purchase history
-- for every ingredient order.
/*
CREATE TABLE IF NOT EXISTS ingredient_purchase(
  ingredientOrder_id
  ingredient_id int,
  amout_purchase float,
  delivery_datetime DATETIME,
  FOREIGN KEY(ingreident_id) REFERENCES ingridients(ingridient_id)
);
*/



