DO $$ BEGIN
  CREATE TYPE lending_min_appreciation_type_enum AS ENUM ('NONE', 'FIXED', 'PCT_OF_PRINCIPAL', 'TERM_TIERED');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

ALTER TABLE lending_pools
  ADD COLUMN IF NOT EXISTS platform_appreciation_share_pct numeric(5,2) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS min_appreciation_type lending_min_appreciation_type_enum NOT NULL DEFAULT 'NONE',
  ADD COLUMN IF NOT EXISTS min_appreciation_value numeric(10,2) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS min_appreciation_tiers json,
  ADD COLUMN IF NOT EXISTS economics_updated_at timestamp,
  ADD COLUMN IF NOT EXISTS economics_updated_by_admin_id integer REFERENCES users(id),
  ADD COLUMN IF NOT EXISTS economics_update_reason text;

ALTER TABLE loan_applications
  ADD COLUMN IF NOT EXISTS platform_appreciation_share_pct numeric(5,2) NOT NULL DEFAULT 0;

