-- migrate:up

CREATE TABLE auth.accounts (
    account_id      bigserial PRIMARY KEY,
    username        text NOT NULL UNIQUE,
    password_hash   text NOT NULL,
    scopes          text[] NOT NULL DEFAULT ARRAY[]::text[],
    is_active       boolean NOT NULL DEFAULT true,
    created_at      timestamptz NOT NULL DEFAULT now(),
    last_login_at   timestamptz
);

CREATE TABLE auth.tokens (
    token_id        bigserial PRIMARY KEY,
    account_id      bigint NOT NULL REFERENCES auth.accounts(account_id),
    token_type      text NOT NULL CHECK (token_type IN ('refresh')),
    token_hash      text NOT NULL UNIQUE,
    issued_at       timestamptz NOT NULL DEFAULT now(),
    expires_at      timestamptz NOT NULL,
    revoked_at      timestamptz
);

CREATE INDEX auth_tokens_account_idx
    ON auth.tokens (account_id)
    WHERE revoked_at IS NULL;

-- migrate:down

DROP TABLE IF EXISTS auth.tokens;
DROP TABLE IF EXISTS auth.accounts;
