CREATE TABLE IF NOT EXISTS third_party_orders (
  id VARCHAR(36) NOT NULL,
  provider VARCHAR(64) NOT NULL,
  external_order_no VARCHAR(160) NOT NULL,
  username VARCHAR(160) NULL,
  school VARCHAR(255) NULL,
  course_name VARCHAR(255) NULL,
  remote_status VARCHAR(120) NULL,
  local_order_id VARCHAR(36) NULL,
  raw_payload JSON NULL,
  first_seen_at DATETIME NOT NULL,
  last_seen_at DATETIME NOT NULL,
  created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (id),
  UNIQUE KEY uk_third_party_orders_provider_external (provider, external_order_no),
  KEY idx_third_party_orders_last_seen (provider, last_seen_at),
  KEY idx_third_party_orders_local_order (local_order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
