import { DataTypes } from "sequelize";

export default (sequelize) => {
  const Customer = sequelize.define("Customer", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    merchantId: {
      type: DataTypes.UUID,
      allowNull: false,
    },
    fullName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: { isEmail: true },
    },
    phoneNumber: {
        type: DataTypes.STRING,
        allowNull: true,
    },
    accountNumber: {
      type: DataTypes.STRING,
      allowNull: true, // 9PSB Virtual Account
    },
    bankName: {
        type: DataTypes.STRING,
        defaultValue: "9PSB"
    },
    balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    status: {
      type: DataTypes.ENUM("active", "inactive", "suspended"),
      defaultValue: "active",
    },
    bvn: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    bvnStatus: {
      type: DataTypes.ENUM("none", "pending", "verified", "failed"),
      defaultValue: "none",
    },
    idType: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    idValue: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    idStatus: {
      type: DataTypes.ENUM("none", "pending", "verified", "failed"),
      defaultValue: "none",
    },
    kycStatus: {
      type: DataTypes.ENUM("none", "pending", "verified", "failed"),
      defaultValue: "none",
    },
    lastActivityAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
  }, {
    timestamps: true,
  });

  Customer.associate = (models) => {
    Customer.belongsTo(models.Merchant, { foreignKey: "merchantId" });
    Customer.hasMany(models.Transaction, { foreignKey: "customerId" });
    Customer.hasMany(models.CustomerServiceBalance, { foreignKey: "customerId", as: "serviceBalances" });
    Customer.hasMany(models.Card, { foreignKey: "customerId", as: "cards" });
    Customer.hasOne(models.PayscribePerson, { foreignKey: "customerId", as: "payscribeProfile" });
  };

  return Customer;
};
