-- Migration 100: domrf_kn_documents — PDF documents scraped from DOM.РФ object pages
-- Issue #297, sub-task 22i. Phase 3 new tables.
-- Stores declarations, permits, project docs, reports, misc PDFs per obj_id.

BEGIN;

CREATE TABLE IF NOT EXISTS domrf_kn_documents (
    id            bigserial PRIMARY KEY,
    obj_id        bigint    NOT NULL,
    doc_type      text      NOT NULL,  -- декларация/разрешение/проектная/отчётность/прочее
    doc_num       text,
    posted_at     date,
    file_url      text      NOT NULL,
    size_bytes    bigint,
    local_path    text,
    downloaded_at timestamptz,
    scraped_at    timestamptz NOT NULL DEFAULT NOW(),
    UNIQUE (obj_id, doc_type, doc_num, file_url)
);

CREATE INDEX IF NOT EXISTS domrf_kn_documents_obj_type_idx
    ON domrf_kn_documents (obj_id, doc_type);

COMMENT ON TABLE domrf_kn_documents IS
    'PDF docs scraped from DOM.RF obj page. Issue #297 22i.';

COMMENT ON COLUMN domrf_kn_documents.doc_type IS
    'Canonical type: декларация / разрешение / проектная / отчётность / прочее';
COMMENT ON COLUMN domrf_kn_documents.doc_num IS
    'Document number as shown on DOM.RF (e.g. permit number, declaration number)';
COMMENT ON COLUMN domrf_kn_documents.file_url IS
    'Full URL to the PDF on наш.дом.рф (CDN or api/ext/file/...)';
COMMENT ON COLUMN domrf_kn_documents.local_path IS
    'Relative path inside data/raw/domrf_docs/ after download (populated by future task)';
COMMENT ON COLUMN domrf_kn_documents.downloaded_at IS
    'Timestamp when PDF was successfully downloaded locally (NULL = not yet downloaded)';

COMMIT;
