CREATE TABLE invite_tokens (
    id            UUID         PRIMARY KEY DEFAULT gen_random_uuid(),
    code          VARCHAR(10)  UNIQUE NOT NULL,
    label         VARCHAR(255),
    max_uses      INTEGER,
    use_count     INTEGER      NOT NULL DEFAULT 0,
    prefill_first_name VARCHAR(255),
    prefill_last_name  VARCHAR(255),
    prefill_email VARCHAR(255),
    expires_at    TIMESTAMP,
    created_by    UUID         NOT NULL REFERENCES users(id),
    created_at    TIMESTAMP    NOT NULL DEFAULT NOW(),
    revoked       BOOLEAN      NOT NULL DEFAULT FALSE
);

CREATE INDEX idx_invite_tokens_code ON invite_tokens(code);

CREATE TABLE invite_token_group_ids (
    invite_token_id UUID NOT NULL REFERENCES invite_tokens(id),
    group_id        UUID NOT NULL REFERENCES user_groups(id),
    PRIMARY KEY (invite_token_id, group_id)
);
