-- migrate:up
-- Extiende audit_log para trazar acciones de operadores con contexto rico.
-- La columna 'actor' (text) preexistente queda como fallback para escrituras
-- del bot/sistema sin user (ej. webhook woo).

ALTER TABLE audit_log
  ADD COLUMN IF NOT EXISTS actor_user_id BIGINT REFERENCES system_users(id) ON DELETE SET NULL,
  ADD COLUMN IF NOT EXISTS actor_email   TEXT,
  ADD COLUMN IF NOT EXISTS actor_ip      INET,
  ADD COLUMN IF NOT EXISTS action_path   TEXT,
  ADD COLUMN IF NOT EXISTS summary       TEXT;

CREATE INDEX IF NOT EXISTS audit_log_created_at_idx ON audit_log (created_at DESC);
CREATE INDEX IF NOT EXISTS audit_log_actor_user_id_idx ON audit_log (actor_user_id);

-- migrate:down
DROP INDEX IF EXISTS audit_log_actor_user_id_idx;
DROP INDEX IF EXISTS audit_log_created_at_idx;
ALTER TABLE audit_log
  DROP COLUMN IF EXISTS summary,
  DROP COLUMN IF EXISTS action_path,
  DROP COLUMN IF EXISTS actor_ip,
  DROP COLUMN IF EXISTS actor_email,
  DROP COLUMN IF EXISTS actor_user_id;
