CREATE TABLE IF NOT EXISTS auth.tb_auth_refresh_token (
    token_id        VARCHAR(64) PRIMARY KEY,
    user_id         VARCHAR(64) NOT NULL,
    username        VARCHAR(64) NOT NULL,
    province_code   VARCHAR(12),
    area_code       VARCHAR(12),
    tenant_id       VARCHAR(64),
    tenant_path     VARCHAR(255),
    dept_id         VARCHAR(64),
    dept_path       VARCHAR(255),
    refresh_token   TEXT NOT NULL,
    expire_at       TIMESTAMP NOT NULL,
    revoked         BOOLEAN NOT NULL DEFAULT FALSE,
    created_at      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS auth.tb_auth_login_audit (
    audit_id        VARCHAR(64) PRIMARY KEY,
    user_id         VARCHAR(64),
    username        VARCHAR(64) NOT NULL,
    province_code   VARCHAR(12),
    area_code       VARCHAR(12),
    tenant_id       VARCHAR(64),
    tenant_path     VARCHAR(255),
    dept_id         VARCHAR(64),
    dept_path       VARCHAR(255),
    login_ip        VARCHAR(64),
    login_status    VARCHAR(32) NOT NULL,
    created_at      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_auth_refresh_token_user ON auth.tb_auth_refresh_token(user_id);
CREATE INDEX IF NOT EXISTS idx_auth_login_audit_tenant ON auth.tb_auth_login_audit(tenant_id, created_at DESC);
