CREATE TABLE order_state_logs (
                                  id BIGINT PRIMARY KEY,
                                  order_id BIGINT NOT NULL REFERENCES orders(id),
                                  from_status VARCHAR(30),
                                  to_status VARCHAR(30) NOT NULL,
                                  action VARCHAR(50) NOT NULL,
                                  actor_id BIGINT NOT NULL,
                                  actor_role VARCHAR(20) NOT NULL,
                                  metadata JSONB,
                                  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_state_logs_order ON order_state_logs(order_id, created_at);
CREATE INDEX idx_state_logs_order_created ON order_state_logs(order_id, created_at DESC);
CREATE INDEX idx_state_logs_actor ON order_state_logs(actor_id, created_at DESC);