import { DataTypes } from "sequelize";

export default (sequelize) => {
  const MerchantWalletBalance = sequelize.define("MerchantWalletBalance", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    merchantId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    currency: {
      type: DataTypes.STRING(10),
      allowNull: false,
      comment: "Currency code, e.g. USD, EUR. NGN is never stored here — it lives on Merchant.balance.",
    },
    balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
      allowNull: false,
    },
    prev_balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM("active", "inactive"),
      defaultValue: "active",
      allowNull: false,
      comment: "Mirrors the owning Currency's virtualAccountStatus — toggled by the daily wallet-currency sync, not deleted.",
    },
  }, {
    tableName: "MerchantWalletBalances",
    timestamps: true,
    indexes: [
      {
        unique: true,
        fields: ["merchantId", "currency"],
      },
    ],
  });

  MerchantWalletBalance.associate = (models) => {
    MerchantWalletBalance.belongsTo(models.Merchant, {
      foreignKey: "merchantId",
      as: "merchant",
    });
  };

  return MerchantWalletBalance;
};
