-- +goose Up
-- +goose StatementBegin
CREATE TABLE
    tenants (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now(),
        expired_at timestamp NOT NULL,

        created_by_user_id INT8 NOT NULL,
        name VARCHAR(128) NOT NULL,
        slug VARCHAR(128) NOT NULL UNIQUE,
        description VARCHAR(128)
    );

-- index on name slug
CREATE INDEX idx_tenants_name ON tenants(name);
CREATE INDEX idx_tenants_slug ON tenants(slug);


CREATE TABLE
    tenant_users (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now (),
        updated_at timestamp NOT NULL default now (),
        deleted_at timestamp,

        tenant_id INT8 NOT NULL,
        user_id INT8 NOT NULL,
        status INT2 NOT NULL default 0,
        role INT2 NOT NULL default 0
    );
-- indexes
CREATE INDEX idx_tenant_users_tenant_id ON tenant_users(tenant_id);
CREATE INDEX idx_tenant_users_user_id ON tenant_users(user_id);
CREATE INDEX idx_tenant_users_role ON tenant_users(role);

-- create tenant user balance
CREATE TABLE
    tenant_user_balances (
        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,
        balance INT8 NOT NULL default 0
    );
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE tenants;
DROP TABLE tenant_users;
-- +goose StatementEnd
