-- Sprint 3 — an audit trail for changes to a vendor's profile.
--
-- The admin vendor form has until now been an in-memory stub: it toasted
-- "Saved" and discarded everything on reload. Making it persist means an admin
-- can change a credit ceiling, a settlement tier or a status for real, and those
-- are decisions someone will later need to account for ("who dropped this
-- vendor to weekly?", "who raised the ceiling to 80k?").
--
-- Mirrors `order_events` (001_init.sql) deliberately: same actor columns, same
-- nullable-on-delete FK to users, same "one row per thing that happened" shape.
-- Scoped to vendors rather than made a generic audit_log, because a generic one
-- would need a polymorphic subject and this codebase has no such pattern yet;
-- riders and bikes can get their own table when they need one.
--
-- `changes` holds only the fields that actually differed, as
-- {"field": {"from": ..., "to": ...}}, so reading a row tells you what moved
-- without diffing against another source.
CREATE TABLE IF NOT EXISTS vendor_events (
  id            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  vendor_id     VARCHAR(8)  NOT NULL,
  type          ENUM('profile_update','paybill_update') NOT NULL,
  actor_user_id BIGINT UNSIGNED NULL,
  actor_role    VARCHAR(16) NULL,
  changes       JSON        NULL,
  note          TEXT        NULL,
  created_at    DATETIME    NOT NULL,
  PRIMARY KEY (id),
  KEY idx_ve_vendor (vendor_id),
  CONSTRAINT fk_ve_vendor FOREIGN KEY (vendor_id)     REFERENCES vendors(id) ON DELETE CASCADE,
  CONSTRAINT fk_ve_user   FOREIGN KEY (actor_user_id) REFERENCES users(id)   ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
