-- migrate:up
create table if not exists product_reco_rules (
  id bigserial primary key,
  tenant_id uuid not null references tenants(id) on delete cascade,
  rule_key text not null,
  trigger jsonb not null default '{}'::jsonb,
  queries jsonb not null default '[]'::jsonb,
  boosts jsonb not null default '{}'::jsonb,
  ask_slots jsonb not null default '[]'::jsonb,
  active boolean not null default true,
  priority integer not null default 100,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  unique (tenant_id, rule_key)
);

create index if not exists product_reco_rules_tenant_idx
  on product_reco_rules (tenant_id);

create index if not exists product_reco_rules_active_idx
  on product_reco_rules (tenant_id, active, priority);

-- migrate:down
drop table if exists product_reco_rules;
