-- Ensure audit_logs has all expected columns
ALTER TABLE audit_logs
  ADD COLUMN IF NOT EXISTS method VARCHAR(10) NULL AFTER username,
  ADD COLUMN IF NOT EXISTS url VARCHAR(1024) NULL AFTER method,
  ADD COLUMN IF NOT EXISTS ip VARCHAR(100) NULL AFTER url,
  ADD COLUMN IF NOT EXISTS user_agent VARCHAR(512) NULL AFTER ip,
  ADD COLUMN IF NOT EXISTS status INT NULL AFTER user_agent,
  ADD COLUMN IF NOT EXISTS action VARCHAR(190) NULL AFTER status,
  ADD COLUMN IF NOT EXISTS resource VARCHAR(190) NULL AFTER action,
  ADD COLUMN IF NOT EXISTS resource_id VARCHAR(190) NULL AFTER resource,
  ADD COLUMN IF NOT EXISTS payload JSON NULL AFTER resource_id,
  ADD COLUMN IF NOT EXISTS created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP AFTER payload;

-- Backfill nullable defaults if needed
UPDATE audit_logs SET status = COALESCE(status, 200);
