const Sequelize = require("sequelize");

module.exports = (db) => {
  const biz_api_call_log = db.define(
    "biz_api_call_log",
    {
      id: {
        type: Sequelize.BIGINT.UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      user_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: false,
        comment: "业务用户ID",
      },
      token_id: {
        type: Sequelize.BIGINT.UNSIGNED,
        allowNull: false,
        comment: "使用的Token ID",
      },
      api_path: {
        type: Sequelize.STRING(200),
        allowNull: false,
        comment: "接口路径，如 /user/GetProfile",
      },
      http_method: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: "POST",
      },
      status_code: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "上游返回的HTTP状态码",
      },
      response_time: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "上游响应耗时ms",
      },
      call_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: "调用日期，方便按天统计",
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    },
    {
      tableName: "biz_api_call_log",
      timestamps: false,
      underscored: true,
      comment: "API调用日志",
      indexes: [
        { fields: ["user_id", "call_date"], name: "idx_user_date" },
        { fields: ["api_path", "call_date"], name: "idx_api_date" },
        { fields: ["user_id", "api_path"], name: "idx_user_api" },
      ],
    }
  );
  return biz_api_call_log;
};
