-- 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 TABLE skill_version IS 'Skill版本表';
