-- +goose Up
-- +goose StatementBegin
-- table users
CREATE TABLE
    users (
        id SERIAL8 PRIMARY KEY,
        open_id VARCHAR(128) NOT NULL UNIQUE,
        union_id VARCHAR(128) ,
        oauth jsonb default '{}'::jsonb,
        expire_in timestamp NOT NULL,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now()
    );

CREATE INDEX idx_users_open_id ON users (open_id);
CREATE INDEX idx_users_union_id ON users (union_id);

-- table tenants
CREATE TABLE
    tenants (
        id SERIAL8 PRIMARY KEY,
        name VARCHAR(128) NOT NULL,
        slug VARCHAR(128) NOT NULL,
        description VARCHAR(128),
        expire_at timestamp NOT NULL,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now()
    );

-- table users_tenants
CREATE TABLE
    users_tenants (
        id SERIAL8 PRIMARY KEY,
        user_id INT8 NOT NULL,
        tenant_id INT8 NOT NULL,
        created_at timestamp NOT NULL default now()
    );

CREATE INDEX idx_users_tenants_user_id ON users_tenants (user_id);
CREATE INDEX idx_users_tenants_tenant_id ON users_tenants (tenant_id);
-- uniq user_id, tenant_id
CREATE UNIQUE INDEX idx_users_tenants_user_id_tenant_id ON users_tenants (user_id, tenant_id);

CREATE TABLE tenant_user_balances (
    id SERIAL8 PRIMARY KEY,
    user_id INT8 NOT NULL,
    tenant_id INT8 NOT NULL,
    balance INT8 NOT NULL
);

CREATE INDEX idx_tenant_user_balance_user_id ON tenant_user_balances (user_id);
CREATE INDEX idx_tenant_user_balance_tenant_id ON tenant_user_balances (tenant_id);

-- table user_balance_history
CREATE TABLE user_balance_histories (
    id SERIAL8 PRIMARY KEY,
    user_id INT8 NOT NULL,
    tenant_id INT8 NOT NULL,
    balance INT8 NOT NULL,
    target jsonb default '{}'::jsonb,
    type VARCHAR(128) NOT NULL, -- charge, consume, refund
    created_at timestamp NOT NULL default now()
);
CREATE INDEX idx_user_balance_histories_user_id ON user_balance_histories (user_id);
CREATE INDEX idx_user_balance_histories_tenant_id ON user_balance_histories (tenant_id);

-- medias
CREATE TABLE
    medias (
        id SERIAL8 PRIMARY KEY,
        tenant_id INT8 NOT NULL,
        title VARCHAR(198) NOT NULL,
        description VARCHAR(198) NOT NULL,
        price INT8 NOT NULL,
        discount INT8 NOT NULL default 100,
        publish BOOL NOT NULL,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now()
    );

CREATE INDEX idx_medias_tenant_id ON medias (tenant_id);

CREATE INDEX idx_medias_title ON medias (title);

CREATE TABLE
    media_resources (
        id SERIAL8 PRIMARY KEY,
        media_id INT8 NOT NULL,
        type VARCHAR(128) NOT NULL,
        source jsonb  default '{}'::jsonb,
        size INT8 NOT NULL,
        publish BOOL NOT NULL,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now()
    );

CREATE INDEX idx_media_resources_media_id ON media_resources (media_id);

CREATE TABLE user_medias (
    id SERIAL8 PRIMARY KEY,
    user_id INT8 NOT NULL,
    tenant_id INT8 NOT NULL,
    media_id INT8 NOT NULL,
    price INT8 NOT NULL,
    created_at timestamp NOT NULL default now()
);

CREATE INDEX idx_user_medias_user_id ON user_medias (user_id);
CREATE INDEX idx_user_medias_tenant_id ON user_medias (tenant_id);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE users;
DROP TABLE tenants;
DROP TABLE users_tenants;
DROP TABLE tenant_user_balances;
DROP TABLE user_balance_histories;
DROP TABLE medias;
DROP TABLE media_resources;
DROP TABLE user_medias;
-- +goose StatementEnd
