CREATE TABLE IF NOT EXISTS order_attempts (
  id serial PRIMARY KEY,
  client_ref text NOT NULL UNIQUE,
  payload_hash text NOT NULL,
  session_id text,
  state text NOT NULL DEFAULT 'creating',
  bot_order_id text,
  moderation_id text,
  bot_state text,
  bot_response jsonb,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS order_attempts_state_idx ON order_attempts(state);
