--------------------------------------------
-- Recipe/Cookie 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)
  -- ...Perhaps more columns
);

-- 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_date date DEFAULT NOW(),
  delivery_date date, -- Set when the order hits the truck
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
  -- ...Perhaps more columns
);

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

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

-- "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),
  amount int,
  unit varchar(50),
);

-- Describes what ingredients goes into what recipe
-- Each recipe requires 'amount' of 'ingredient'
CREATE TABLE IF NOT EXISTS recipe_contents (
  recipe_id int,
  ingredient_id int,
  amount int,
  PRIMARY KEY (recipe_id, ingredient_id),
);

--------------------------------------------
-- Pallet related tables
--------------------------------------------

-- Pallets are used to store cookies for delivery
CREATE TABLE IF NOT EXISTS pallets (
  pallet_id int PRIMARY KEY,
  recipe_id int,
  order_id int,
  FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id),
  FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);

-- What does the pallet contain?
CREATE TABLE IF NOT EXISTS pallet_contents (
  pallet_id int,
  ingredient_id int,
  amount int,
  PRIMARY KEY (pallet_id, ingredient_id),
  FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id),
  FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id)
);

-- Has an order been delivered?
-- When the truck is loaded, a delivery is considered done
CREATE TABLE IF NOT EXISTS delivery_bill (
  delivery_id int PRIMARY KEY,
  order_id int,
  delivery_date date DEFAULT NOW(),
  FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);