-- +goose Up
-- +goose StatementBegin
-- create posts table
CREATE TABLE
    posts (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now(),
        deleted_at timestamp,

        type INT2 NOT NULL default 0,
        stage INT2 NOT NULL default 0,
        status INT2 NOT NULL default 0,

        tenant_id INT8 NOT NULL,
        user_id INT8 NOT NULL,

        title VARCHAR(128) NOT NULL,
        description VARCHAR(256) NOT NULL,
        content TEXT NOT NULL,
        price INT8 NOT NULL default 0,
        discount INT2 NOT NULL default 100,
        views INT8 NOT NULL default 0,
        likes INT8 NOT NULL default 0,
        meta jsonb default '{}'::jsonb,
        tags jsonb default '{}'::jsonb,
        assets jsonb default '{}'::jsonb
    );
-- create indexes
CREATE INDEX posts_type_index ON posts (type);
CREATE INDEX posts_tenant_id_index ON posts (tenant_id);
CREATE INDEX posts_user_id_index ON posts (user_id);
CREATE INDEX posts_title_index ON posts (title);


-- create user bought posts
CREATE TABLE
    user_bought_posts (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now(),

        tenant_id INT8 NOT NULL,
        user_id INT8 NOT NULL,
        post_id INT8 NOT NULL,
        price INT8 NOT NULL default 0,
        discount INT2 NOT NULL default 100,
        meta jsonb default '{}'::jsonb
    );
-- create indexes
CREATE INDEX user_bought_posts_tenant_id_index ON user_bought_posts (tenant_id);
CREATE INDEX user_bought_posts_user_id_index ON user_bought_posts (user_id);
CREATE INDEX user_bought_posts_post_id_index ON user_bought_posts (post_id);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE posts;
DROP TABLE user_bought_posts;
-- +goose StatementEnd
