-- 断链焊接（第二段）：配合力纯Python化 / 选择指数落库 / 亲缘近交分析
-- 新增 bre_selection_index 表（create_all 只建新表，已有库需手动执行本脚本）。
-- 依赖：bre_selection_result 表已存在（决选写入）；bre_germplasm/bre_tree 已存在。

CREATE TABLE IF NOT EXISTS bre_selection_index (
    id                 integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    model_name         varchar(128),
    method             varchar(16),
    weights_json       jsonb,
    batch_refs_json    jsonb,
    heritability_json  jsonb,
    top_n              integer,
    result_json        jsonb,
    created_time       timestamp with time zone NOT NULL DEFAULT now(),
    updated_time       timestamp with time zone,
    is_deleted         boolean NOT NULL DEFAULT false,
    deleted_time       timestamp with time zone,
    created_id         integer REFERENCES sys_user(id) ON UPDATE CASCADE ON DELETE SET NULL,
    updated_id         integer REFERENCES sys_user(id) ON UPDATE CASCADE ON DELETE SET NULL,
    deleted_id         integer REFERENCES sys_user(id) ON UPDATE CASCADE ON DELETE SET NULL,
    uuid               varchar(64) NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS ix_bre_selection_index_uuid ON bre_selection_index (uuid);
CREATE INDEX IF NOT EXISTS ix_bre_selection_index_created_deleted ON bre_selection_index (created_time, is_deleted);
CREATE INDEX IF NOT EXISTS ix_bre_selection_index_created_id ON bre_selection_index (created_id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_index_updated_id ON bre_selection_index (updated_id);
CREATE INDEX IF NOT EXISTS ix_bre_selection_index_deleted_id ON bre_selection_index (deleted_id);
