-- KBX v4 reference projections. In production these can be maintained by application writes,
-- triggers, or projectors; the UX contract only requires stable read models.
create table if not exists erp_item_search_projection (
  id uuid primary key,
  code varchar(50) not null,
  name varchar(200) not null,
  category_name varchar(120) not null default '',
  specification varchar(200) not null default '',
  unit varchar(20) not null default 'EA',
  barcode varchar(100) not null default '',
  default_warehouse_id uuid null,
  default_warehouse_name varchar(120) not null default '',
  lot_managed boolean not null default false,
  expiry_managed boolean not null default false,
  active boolean not null default true,
  version bigint not null default 1,
  search_text text not null default ''
);
create table if not exists erp_inventory_snapshot_projection (
  snapshot_key varchar(100) primary key,
  item_id uuid not null,
  item_code varchar(50) not null,
  item_name varchar(200) not null,
  specification varchar(200) not null default '',
  warehouse_id uuid not null,
  warehouse_name varchar(120) not null,
  location_id uuid null,
  location_code varchar(80) null,
  on_hand_qty numeric(18,4) not null default 0,
  allocated_qty numeric(18,4) not null default 0,
  hold_qty numeric(18,4) not null default 0,
  available_qty numeric(18,4) not null default 0,
  search_text text not null default ''
);
create index if not exists ix_erp_inventory_snapshot_item on erp_inventory_snapshot_projection(item_id);
