const Sequelize = require("sequelize");

module.exports = (db) => {
  const biz_api_token = db.define(
    "biz_api_token",
    {
      id: {
        type: Sequelize.BIGINT.UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      user_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: false,
      },
      plan_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: true,
        comment: "冗余：鉴权时少联表",
      },
      token_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        defaultValue: "",
      },
      token_hash: {
        type: Sequelize.STRING(64),
        allowNull: false,
        unique: true,
      },
      status: {
        type: Sequelize.ENUM("active", "revoked", "expired"),
        allowNull: false,
        defaultValue: "active",
      },
      expire_at: { type: Sequelize.DATE, allowNull: false },
      last_used_at: { type: Sequelize.DATE, allowNull: true },
    },
    {
      tableName: "biz_api_tokens",
      timestamps: false,
      underscored: true,
      comment: "API Token",
    }
  );
  // biz_api_token.sync({ alter: true });
  return biz_api_token;
};
