CREATE TABLE IF NOT EXISTS customers (
  customer_id int PRIMARY KEY,
  name varchar(100),
  address varchar(255)
);

CREATE TABLE IF NOT EXISTS products (
  product_id int PRIMARY KEY,
  name varchar(100)
);

CREATE TABLE IF NOT EXISTS recipes (
  recipe_name varchar(100),
  recipe_year year,
  ingredients int,
  product_id int,
  PRIMARY KEY (recipe_name, recipe_year),
  FOREIGN KEY (ingredients) REFERENCES ingredients(ingredient_id),
  FOREIGN KEY (product_id) REFERENCES products(product_id)
);

CREATE TABLE IF NOT EXISTS ingredients (
  ingredient_id int PRIMARY KEY,
  raw_material_name varchar(100),
  amount int,
  unit varchar(50),
  FOREIGN KEY (raw_material_name) REFERENCES raw_materials(raw_material_name)
);

CREATE TABLE IF NOT EXISTS raw_materials (
  raw_material_name varchar(100) PRIMARY KEY,
  quantity int,
  last_delivery_date_time datetime
);

CREATE TABLE IF NOT EXISTS pallets_produced (
  pallet_id int PRIMARY KEY,
  product_id int,
  production_date_time datetime,
  FOREIGN KEY (product_id) REFERENCES products (product_id)
);

CREATE TABLE IF NOT EXISTS pallets_delivered (
  delivered_id int PRIMARY KEY,
  pallet_id int,
  delivery_date_time datetime,
  FOREIGN KEY (pallet_id) REFERENCES pallets_produced (pallet_id),
  FOREIGN KEY (delivered_id) REFERENCES truck (pallet)
);

CREATE TABLE IF NOT EXISTS truck (
  truck_id int PRIMARY KEY,
  capacity int,
  pallet int
);

CREATE TABLE IF NOT EXISTS loading_bill (
  loadingbill_id int PRIMARY KEY,
  address varchar(100),
  customer varchar(100),
  truck_id int,
  FOREIGN KEY (truck_id) REFERENCES truck (truck_id)
);

CREATE TABLE IF NOT EXISTS orders (
  order_id int PRIMARY KEY,
  customer_id int,
  product_id int,
  quantity int,
  order_date_time datetime,
  FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
  FOREIGN KEY (product_id) REFERENCES products (product_id)
);

CREATE TABLE IF NOT EXISTS blocked_products (
  blocked_product_id int PRIMARY KEY,
  product_id int,
  blocked_date_time datetime,
  FOREIGN KEY (product_id) REFERENCES products (product_id)
);

CREATE TABLE IF NOT EXISTS pallet_traceability (
  trace_id int PRIMARY KEY,
  location varchar(100),
  location_date datetime,
  pallet_id int,
  FOREIGN KEY (pallet_id) REFERENCES pallets_produced (pallet_id)
);
