CREATE TABLE IF NOT EXISTS clyo_bridge_orders (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_commande BIGINT UNSIGNED NOT NULL,
  clyo_password VARCHAR(100) NOT NULL,
  payload_line LONGTEXT NOT NULL,
  status ENUM('NEW','DELIVERED','ACKED','ERROR') NOT NULL DEFAULT 'NEW',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  delivered_at DATETIME NULL,
  acked_at DATETIME NULL,
  last_error VARCHAR(255) NULL,
  UNIQUE KEY uk_id_commande (id_commande),
  KEY idx_password_status_created (clyo_password, status, created_at),
  KEY idx_status_created (status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS clyo_bridge_log (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  id_commande BIGINT UNSIGNED NULL,
  event_type VARCHAR(50) NOT NULL,
  details TEXT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_event_created (event_type, created_at),
  KEY idx_order_created (id_commande, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
