CREATE TABLE library
(
    id          UUID PRIMARY KEY,
    name        VARCHAR(255) NOT NULL,
    description TEXT,
    created_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE TABLE library_alias
(
    id         UUID PRIMARY KEY,
    library_id UUID         NOT NULL REFERENCES library (id) ON DELETE CASCADE,
    alias      VARCHAR(255) NOT NULL,
    UNIQUE (library_id, alias)
);

CREATE INDEX idx_library_alias_lookup ON library_alias (alias);

CREATE TABLE library_symbol
(
    id          UUID PRIMARY KEY,
    library_id  UUID         NOT NULL REFERENCES library (id) ON DELETE CASCADE,
    module_name VARCHAR(255) NOT NULL,
    class_name  VARCHAR(500),
    symbol_name VARCHAR(500) NOT NULL,
    ordinal     INTEGER      NOT NULL,
    symbol_type VARCHAR(50),
    parameters  VARCHAR(1000),
    category    VARCHAR(100),
    created_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_library_symbol_library_ordinal ON library_symbol (library_id, ordinal);
CREATE INDEX idx_library_symbol_class ON library_symbol (class_name) WHERE class_name IS NOT NULL;
