-- Dashboard / Orders & Tracking / Reorder Centre tables (table prefix UOYP7Y_).
-- Run once in phpMyAdmin. The theme checks the tables exist before using them.

-- One row per customer + product: how often and how much they order it.
-- Written once per order when it is paid; drives Reorder Centre and "Time to restock".
CREATE TABLE IF NOT EXISTS UOYP7Y_c2p_customer_products (
  customer_id      BIGINT UNSIGNED NOT NULL,
  product_id       BIGINT UNSIGNED NOT NULL,
  times_ordered    INT UNSIGNED    NOT NULL DEFAULT 0,
  total_qty        INT UNSIGNED    NOT NULL DEFAULT 0,
  usual_qty        INT UNSIGNED    NOT NULL DEFAULT 1,
  qty_hist         VARCHAR(255)    NOT NULL DEFAULT '',
  last_qty         INT UNSIGNED    NOT NULL DEFAULT 1,
  last_order_id    BIGINT UNSIGNED NOT NULL DEFAULT 0,
  last_ordered_at  DATETIME NULL,
  first_ordered_at DATETIME NULL,
  PRIMARY KEY (customer_id, product_id),
  KEY customer_rank (customer_id, times_ordered),
  KEY customer_last (customer_id, last_ordered_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

-- Parcels for an order. Written by the "Tracking" box on the admin order screen
-- or from your shipping plugin with c2p_oms_set_tracking($order_id, [...]).
CREATE TABLE IF NOT EXISTS UOYP7Y_c2p_order_tracking (
  id              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  order_id        BIGINT UNSIGNED NOT NULL,
  carrier         VARCHAR(100) NULL,
  tracking_number VARCHAR(100) NULL,
  tracking_url    VARCHAR(500) NULL,
  shipped_at      DATETIME NULL,
  eta             VARCHAR(100) NULL,
  delivered_at    DATETIME NULL,
  status          VARCHAR(30)  NOT NULL DEFAULT 'shipped',
  note            TEXT NULL,
  created_at      DATETIME NULL,
  updated_at      DATETIME NULL,
  PRIMARY KEY (id),
  KEY order_id (order_id),
  KEY tracking_number (tracking_number)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
