-- KBX v26: independent T04 Golden Screen + T05 inventory history projection.
create table if not exists erp.item_prices (
  id uuid primary key,
  item_id uuid not null references catalog.items(id),
  effective_date date not null,
  unit_price numeric(18,4) not null check(unit_price >= 0),
  remark varchar(500),
  version bigint not null default 1 check(version > 0),
  created_at timestamptz not null default now(),
  created_by varchar(200) not null,
  updated_at timestamptz,
  updated_by varchar(200),
  unique(item_id,effective_date)
);
create index if not exists ix_erp_item_prices_effective on erp.item_prices(item_id,effective_date desc);

create table if not exists erp_inventory_ledger_projection (
  entry_id uuid primary key,
  item_id uuid not null,
  occurred_at timestamptz not null,
  business_type varchar(80) not null,
  reference_no varchar(120) not null default '',
  warehouse_name varchar(120) not null,
  location_code varchar(80),
  inbound_qty numeric(18,4) not null default 0 check(inbound_qty >= 0),
  outbound_qty numeric(18,4) not null default 0 check(outbound_qty >= 0),
  balance_qty numeric(18,4) not null default 0,
  actor varchar(200) not null default 'system'
);
create index if not exists ix_erp_inventory_ledger_item_time on erp_inventory_ledger_projection(item_id,occurred_at desc);
