CREATE TABLE IF NOT EXISTS admin_audit_events (
  id serial PRIMARY KEY,
  actor text NOT NULL,
  action text NOT NULL,
  entity_type text,
  entity_id text,
  details jsonb,
  created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS admin_audit_events_created_idx ON admin_audit_events (created_at DESC);

CREATE TABLE IF NOT EXISTS sbis_sync_state (
  id integer PRIMARY KEY CHECK (id = 1),
  last_attempt_at timestamptz,
  last_success_at timestamptz,
  last_duration_ms integer,
  last_error text,
  last_snapshot_id text,
  source_version text,
  items_count integer NOT NULL DEFAULT 0,
  categories_count integer NOT NULL DEFAULT 0,
  updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO sbis_sync_state (id) VALUES (1) ON CONFLICT (id) DO NOTHING;
