-- Skill 定义与版本表。

CREATE TABLE IF NOT EXISTS skill_definition (
    id BIGSERIAL PRIMARY KEY,
    skill_code VARCHAR(100) NOT NULL,
    skill_name VARCHAR(200) NOT NULL,
    skill_type VARCHAR(50) NOT NULL,
    description VARCHAR(1000),
    status VARCHAR(50) NOT NULL DEFAULT 'DRAFT',
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_skill_definition_code UNIQUE (skill_code)
);

CREATE TABLE IF NOT EXISTS skill_version (
    id BIGSERIAL PRIMARY KEY,
    skill_id BIGINT NOT NULL,
    version_no INTEGER NOT NULL,
    prompt_text TEXT,
    code_text TEXT,
    config_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    variable_schema_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    test_result_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    publish_status VARCHAR(50) NOT NULL DEFAULT 'DRAFT',
    published_time TIMESTAMP,
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_skill_version_no UNIQUE (skill_id, version_no),
    CONSTRAINT fk_skill_version_skill_id FOREIGN KEY (skill_id) REFERENCES skill_definition (id)
);

CREATE INDEX IF NOT EXISTS idx_skill_definition_status ON skill_definition (status);
CREATE INDEX IF NOT EXISTS idx_skill_definition_type ON skill_definition (skill_type);
CREATE INDEX IF NOT EXISTS idx_skill_version_skill_id ON skill_version (skill_id);
CREATE INDEX IF NOT EXISTS idx_skill_version_publish_status ON skill_version (publish_status);

COMMENT ON TABLE skill_definition IS 'Skill定义表';
COMMENT ON COLUMN skill_definition.id IS 'ID';
COMMENT ON COLUMN skill_definition.skill_code IS 'Skill编码';
COMMENT ON COLUMN skill_definition.skill_name IS 'Skill名称';
COMMENT ON COLUMN skill_definition.skill_type IS 'Skill类型';
COMMENT ON COLUMN skill_definition.description IS 'Skill描述';
COMMENT ON COLUMN skill_definition.status IS '状态';
COMMENT ON COLUMN skill_definition.version IS '版本';
COMMENT ON COLUMN skill_definition.create_time IS '创建时间';
COMMENT ON COLUMN skill_definition.update_time IS '更新时间';
COMMENT ON COLUMN skill_definition.remark IS '备注';
COMMENT ON COLUMN skill_definition.create_by IS '创建者';
COMMENT ON COLUMN skill_definition.update_by IS '更新者';
COMMENT ON TABLE skill_version IS 'Skill版本表';
COMMENT ON COLUMN skill_version.id IS 'ID';
COMMENT ON COLUMN skill_version.skill_id IS 'Skill定义ID';
COMMENT ON COLUMN skill_version.version_no IS '版本号';
COMMENT ON COLUMN skill_version.prompt_text IS '提示词内容';
COMMENT ON COLUMN skill_version.code_text IS '代码内容';
COMMENT ON COLUMN skill_version.config_json IS '运行配置JSON';
COMMENT ON COLUMN skill_version.variable_schema_json IS '变量Schema JSON';
COMMENT ON COLUMN skill_version.test_result_json IS '测试结果JSON';
COMMENT ON COLUMN skill_version.publish_status IS '发布状态';
COMMENT ON COLUMN skill_version.published_time IS '发布时间';
COMMENT ON COLUMN skill_version.version IS '版本';
COMMENT ON COLUMN skill_version.create_time IS '创建时间';
COMMENT ON COLUMN skill_version.update_time IS '更新时间';
COMMENT ON COLUMN skill_version.remark IS '备注';
COMMENT ON COLUMN skill_version.create_by IS '创建者';
COMMENT ON COLUMN skill_version.update_by IS '更新者';
