import { DataTypes } from "sequelize";

export default (sequelize) => {
  const CustomerServiceBalance = sequelize.define("CustomerServiceBalance", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    customerId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    serviceId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
      allowNull: false,
    },
    prev_balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
      allowNull: false,
    },
  }, {
    tableName: "CustomerServiceBalances",
    timestamps: true,
    indexes: [
      {
        unique: true,
        fields: ["customerId", "serviceId"],
      },
    ],
  });

  CustomerServiceBalance.associate = (models) => {
    CustomerServiceBalance.belongsTo(models.Customer, {
      foreignKey: "customerId",
      as: "customer",
    });
    CustomerServiceBalance.belongsTo(models.SystemService, {
      foreignKey: "serviceId",
      as: "service",
    });
  };

  return CustomerServiceBalance;
};
