-- Migration 018: MediaWiki Import Tracking Tables
-- Tracks import runs and cross-references between Wiki pages and local DB entries

CREATE TABLE IF NOT EXISTS wiki_import_log (
    id          SERIAL PRIMARY KEY,
    import_type VARCHAR(50) NOT NULL
                CHECK (import_type IN ('exercise', 'skill', 'method', 'maturity_model')),
    import_status VARCHAR(20) NOT NULL DEFAULT 'running'
                CHECK (import_status IN ('running', 'completed', 'failed')),
    category    VARCHAR(200),                          -- Wiki category name used
    dry_run     BOOLEAN DEFAULT false,
    reimport    BOOLEAN DEFAULT false,
    items_total   INT DEFAULT 0,
    items_imported INT DEFAULT 0,
    items_skipped  INT DEFAULT 0,
    items_failed   INT DEFAULT 0,
    error_log   JSONB DEFAULT '[]'::jsonb,             -- [{item, error}, ...]
    imported_by INT REFERENCES profiles(id),
    started_at  TIMESTAMP DEFAULT NOW(),
    finished_at TIMESTAMP,
    created_at  TIMESTAMP DEFAULT NOW(),
    updated_at  TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS wiki_import_references (
    id             SERIAL PRIMARY KEY,
    wiki_page_title VARCHAR(500) NOT NULL,
    wiki_page_id   INT,                               -- MediaWiki internal page ID
    content_type   VARCHAR(50) NOT NULL
                   CHECK (content_type IN ('exercise', 'skill', 'method', 'maturity_model')),
    local_id       INT NOT NULL,                      -- ID in the local DB table
    last_imported  TIMESTAMP DEFAULT NOW(),
    created_at     TIMESTAMP DEFAULT NOW(),
    UNIQUE(wiki_page_title, content_type)
);

CREATE INDEX IF NOT EXISTS idx_wiki_refs_title ON wiki_import_references(wiki_page_title);
CREATE INDEX IF NOT EXISTS idx_wiki_refs_type  ON wiki_import_references(content_type);
CREATE INDEX IF NOT EXISTS idx_wiki_log_status ON wiki_import_log(import_status);
CREATE INDEX IF NOT EXISTS idx_wiki_log_type   ON wiki_import_log(import_type);
