-- +goose Up

CREATE TABLE workspaces (
    id BIGSERIAL PRIMARY KEY,
    name varchar(255),
    user_id varchar(255),
    created_at timestamp default now(),
    updated_at timestamp default now(),
    deleted_at timestamp default null
);

CREATE INDEX workspaces_user_id_idx ON workspaces (user_id);
CREATE INDEX workspaces_deleted_at_idx ON workspaces (deleted_at);

CREATE TABLE workspace_members (
    id BIGSERIAL PRIMARY KEY,
    workspace_id bigint REFERENCES workspaces(id),
    user_id varchar(255),
--     role
--     role varchar(255),
--     permission varchar(255),
    created_at timestamp default now(),
    updated_at timestamp default now()
);

CREATE TABLE collections (
    id BIGSERIAL PRIMARY KEY,
    name varchar(255),
    workspace_id bigint REFERENCES workspaces(id),
    created_at timestamp default now(),
    updated_at timestamp default now(),
    deleted_at timestamp default null
);
CREATE INDEX collections_workspace_id_idx ON collections (workspace_id);
CREATE INDEX collections_deleted_at_idx ON collections (deleted_at);

CREATE TABLE documents (
    id BIGSERIAL PRIMARY KEY,
    name varchar(255),
    workspace_id bigint REFERENCES workspaces(id),
    collection_id bigint REFERENCES collections(id),
    created_at timestamp default now(),
    updated_at timestamp default now(),
    deleted_at timestamp default null
);

CREATE INDEX documents_workspace_id_idx ON documents (workspace_id);
CREATE INDEX documents_collection_id_idx ON documents (collection_id);
CREATE INDEX documents_deleted_at_idx ON documents (deleted_at);

CREATE TABLE document_blocks (
    id BIGSERIAL PRIMARY KEY,
    document_id bigint REFERENCES documents(id),
    type varchar(255),
    hash varchar(255),
    content text,
    created_at timestamp default now(),
    updated_at timestamp default now()
);

CREATE INDEX document_blocks_document_id_idx ON document_blocks (document_id);
CREATE INDEX document_blocks_type_idx ON document_blocks (type);
CREATE INDEX document_blocks_hash_idx ON document_blocks (hash);

CREATE TABLE block_chunks (
    id BIGSERIAL PRIMARY KEY,
    block_id bigint REFERENCES document_blocks(id),
    content text
);

CREATE INDEX block_chunks_block_id_idx ON block_chunks (block_id);



-- +goose Down

DROP TABLE IF EXISTS block_chunks;
DROP TABLE IF EXISTS document_blocks;
DROP TABLE IF EXISTS documents;
DROP TABLE IF EXISTS collections;
DROP TABLE IF EXISTS workspace_members;
DROP TABLE IF EXISTS workspaces;

