-- Replace JSONB `extra` column in domrf_sold_ready_breakdown with explicit
-- typed columns. Each chart_type has exactly one discriminator field:
--   • foChart            → fo_cd (already existed)
--   • regionChart        → region_cd
--   • devChart           → dev_calc_group_id (e.g. '6072_0' = Самолет)
--   • devValueChart      → dev_group_value  ('do10','do25','do50',...)
--   • cityPopulationChart→ city_population  ('do50','do100','do250',...)
--   • classChart         → obj_class        ('typical','comfort','business','elite')
--   • readyYearChart     → ready_year (INT, plan year)
-- The extra JSONB was a dumping ground for these — split them out.

BEGIN;

ALTER TABLE domrf_sold_ready_breakdown
    ADD COLUMN IF NOT EXISTS region_cd          INT,
    ADD COLUMN IF NOT EXISTS dev_calc_group_id  TEXT,
    ADD COLUMN IF NOT EXISTS dev_group_value    TEXT,
    ADD COLUMN IF NOT EXISTS city_population    TEXT,
    ADD COLUMN IF NOT EXISTS obj_class          TEXT,
    ADD COLUMN IF NOT EXISTS ready_year         INT;

ALTER TABLE domrf_sold_ready_breakdown DROP COLUMN IF EXISTS extra;

COMMENT ON COLUMN domrf_sold_ready_breakdown.region_cd IS
    'Rosstat region code, populated for chart_type=regionChart';
COMMENT ON COLUMN domrf_sold_ready_breakdown.dev_calc_group_id IS
    'Developer ID (matches domrf_developers.developer_id), for chart_type=devChart';
COMMENT ON COLUMN domrf_sold_ready_breakdown.dev_group_value IS
    'Size bucket key for chart_type=devValueChart (do10/do25/do50/...)';
COMMENT ON COLUMN domrf_sold_ready_breakdown.city_population IS
    'Population bucket key for chart_type=cityPopulationChart (do50/do100/...)';
COMMENT ON COLUMN domrf_sold_ready_breakdown.obj_class IS
    'Housing class for chart_type=classChart (typical/comfort/business/elite)';
COMMENT ON COLUMN domrf_sold_ready_breakdown.ready_year IS
    'Plan-completion year for chart_type=readyYearChart';

COMMIT;
