-- DOM.RF analytics raw API endpoint snapshots.
-- Universal JSONB store: one row per (snapshot_date, section, endpoint).
-- Section = page name on наш.дом.рф (launch / share_construction / escrow / ...).
-- Endpoint = sanitized path+query of /api/* call.

CREATE TABLE IF NOT EXISTS domrf_raw_endpoints (
    snapshot_date    DATE NOT NULL,
    section          TEXT NOT NULL,           -- 'launch' | 'sold_ready' | 'quartirografia' | ...
    endpoint         TEXT NOT NULL,           -- 'launch_developers_app__calculationType_SQUARE_repYear_2026_size_1000'
    source_url       TEXT,                    -- inferred original URL (best effort)
    payload          JSONB NOT NULL,
    payload_size     INT,                     -- bytes (raw text length)
    fetched_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (snapshot_date, section, endpoint)
);

CREATE INDEX IF NOT EXISTS idx_domrf_raw_section ON domrf_raw_endpoints (section);
CREATE INDEX IF NOT EXISTS idx_domrf_raw_payload_gin ON domrf_raw_endpoints USING GIN (payload);
