//! Private catalog for resumable pgvector ownership conversions.
//!
//! These operational rows are intentionally not registered with
//! `pg_extension_config_dump`: a dump/restore must discard in-flight jobs
//! rather than resume them against stale relation and type OIDs.

pgrx::extension_sql!(
    r#"
CREATE TABLE pgcontext._pgvector_ownership_conversions (
    conversion_id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    owner_role oid NOT NULL,
    source_table_oid oid NOT NULL,
    source_schema_name name NOT NULL,
    source_table_name name NOT NULL,
    source_column_name name NOT NULL,
    source_attnum int2 NOT NULL CHECK (source_attnum > 0),
    source_type_oid oid NOT NULL,
    source_type_name text NOT NULL CHECK (source_type_name <> ''),
    source_typmod int4 NOT NULL CHECK (source_typmod >= -1),
    shadow_column_name name NOT NULL,
    shadow_attnum int2 CHECK (shadow_attnum IS NULL OR shadow_attnum > 0),
    backup_column_name name NOT NULL,
    trigger_name name NOT NULL,
    index_name name NOT NULL,
    mode text NOT NULL CHECK (mode IN ('fast', 'restricted_online')),
    metric text NOT NULL CHECK (metric IN ('l2', 'inner_product', 'cosine', 'l1')),
    status text NOT NULL DEFAULT 'planned' CHECK (
        status IN (
            'planned',
            'backfilling',
            'index_pending',
            'ready',
            'cutover',
            'completed',
            'rolled_back',
            'failed'
        )
    ),
    dependency_manifest text[] NOT NULL DEFAULT ARRAY[]::text[],
    validation_attestations text[] NOT NULL DEFAULT ARRAY[]::text[],
    total_rows bigint NOT NULL DEFAULT 0 CHECK (total_rows >= 0),
    processed_rows bigint NOT NULL DEFAULT 0 CHECK (processed_rows >= 0),
    mismatch_count bigint NOT NULL DEFAULT 0 CHECK (mismatch_count >= 0),
    backfill_cursor text NOT NULL DEFAULT '(0,0)',
    source_checksum text,
    shadow_checksum text,
    error_message text,
    created_at timestamptz NOT NULL DEFAULT pg_catalog.now(),
    started_at timestamptz,
    updated_at timestamptz NOT NULL DEFAULT pg_catalog.now(),
    completed_at timestamptz,
    CHECK (source_schema_name <> ''),
    CHECK (source_table_name <> ''),
    CHECK (source_column_name <> ''),
    CHECK (shadow_column_name <> backup_column_name),
    CHECK (trigger_name <> index_name),
    CHECK (processed_rows <= total_rows),
    CHECK (completed_at IS NULL OR status IN ('completed', 'rolled_back'))
);

CREATE UNIQUE INDEX pgcontext_pgvector_ownership_conversions_active_source
    ON pgcontext._pgvector_ownership_conversions (source_table_oid, source_attnum)
    WHERE status IN (
        'planned',
        'backfilling',
        'index_pending',
        'ready',
        'cutover',
        'failed'
    );

REVOKE ALL ON TABLE pgcontext._pgvector_ownership_conversions FROM PUBLIC;

CREATE VIEW pgcontext._visible_pgvector_ownership_conversions
WITH (security_barrier = true) AS
SELECT conversions.*
  FROM pgcontext._pgvector_ownership_conversions AS conversions
 WHERE pg_catalog.pg_has_role(SESSION_USER, conversions.owner_role, 'MEMBER');

GRANT SELECT ON pgcontext._visible_pgvector_ownership_conversions TO PUBLIC;
"#,
    name = "create_pgvector_ownership_catalog",
    requires = ["pgcontext_bootstrap"]
);
