-- migrate:up

create table if not exists audit_log (
  id bigserial primary key,
  tenant_id uuid not null references tenants(id) on delete cascade,
  entity_type text not null,        -- 'product' | 'user' | 'order'
  entity_id text not null,          -- woo_product_id, wa_chat_id, etc.
  action text not null,             -- 'create' | 'update' | 'delete' | 'sync_from_woo' | 'push_to_woo'
  changes jsonb,                    -- { field: { old, new } }
  actor text not null default 'system',  -- 'system' | 'webhook' | 'ui'
  created_at timestamptz not null default now()
);

create index if not exists audit_log_entity_idx on audit_log(tenant_id, entity_type, entity_id);
create index if not exists audit_log_created_idx on audit_log(tenant_id, created_at desc);

-- migrate:down
drop table if exists audit_log;
