-- Income-statement subgroups (Buildium-style): each income/expense account can
-- carry a free-text category ("Operating Income", "Administration", "Utilities",
-- "Reserves Budget", …). The Income Statement groups accounts under it with a
-- "Total for <category>" subtotal. Sourced from Buildium's parent-GL hierarchy
-- and editable in the Chart of Accounts. Null = ungrouped.
alter table accounting.accounts
  add column if not exists category text;

-- Seed from the local chart_of_accounts parent hierarchy where it exists
-- (a partial first pass — mostly a few income groups; the Buildium pull fills the rest).
update accounting.accounts a
set category = parent.account_name
from accounting.companies c
join public.chart_of_accounts coa
  on coa.association_id = c.association_id
join public.chart_of_accounts parent
  on parent.id = coa.parent_account_id
where a.company_id = c.id
  and coa.account_number = a.code
  and a.type in ('income','expense')
  and a.category is null
  and parent.account_name is not null;
