import { DataTypes } from "sequelize";

export default (sequelize) => {
  const Merchant = sequelize.define("Merchant", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    businessName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    preview: {
      type: DataTypes.STRING,
      allowNull: true, // Custom prefix for virtual accounts
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: { isEmail: true },
    },
    merchantAccountNumber: {
      type: DataTypes.STRING,
      allowNull: true, // Unique account number assigned for funding
    },
    bankName: {
        type: DataTypes.STRING,
        defaultValue: "9PSB"
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    publicKey: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true,
    },
    privateKey: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    webhookUrl: {
      type: DataTypes.STRING,
      allowNull: true,
      validate: { isUrl: true },
    },
    customDepositFeePercent: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
    },
    customDepositFeeCap: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
    },
    customPayoutFeeFlat: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
    },
    balance: {
      type: DataTypes.DECIMAL(20, 2),
      defaultValue: 0.00,
    },
    status: {
      type: DataTypes.ENUM("active", "inactive", "suspended"),
      defaultValue: "active",
    },
    twoFaEnabled: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
    },
    passwordResetToken: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    passwordResetExpires: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    lastLoginIp: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    lastActivityAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
    },
    isLiveMode: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      allowNull: false,
    },
    kybStatus: {
      type: DataTypes.ENUM("unsubmitted", "pending", "in_review", "approved", "rejected"),
      defaultValue: "unsubmitted",
      allowNull: false,
    },
    livePublicKey: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true,
    },
    livePrivateKey: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  }, {
    timestamps: true,
  });

  Merchant.associate = (models) => {
    Merchant.hasMany(models.Customer, { foreignKey: "merchantId" });
    Merchant.hasMany(models.Transaction, { foreignKey: "merchantId" });
    Merchant.hasMany(models.AuditLog, { foreignKey: "userId", constraints: false });
    Merchant.hasMany(models.MerchantIpWhitelist, { foreignKey: "merchantId", as: "ipWhitelists" });
    Merchant.hasMany(models.MerchantSubscription, { foreignKey: "merchantId", as: "subscriptions" });
    Merchant.hasMany(models.SubscriptionInvoice, { foreignKey: "merchantId", as: "subscriptionInvoices" });
    Merchant.hasOne(models.KybApplication, { foreignKey: "merchantId", as: "kybApplication" });
    Merchant.hasMany(models.MerchantWalletBalance, { foreignKey: "merchantId", as: "wallets" });
    Merchant.hasMany(models.MerchantServicePricing, { foreignKey: "merchantId", as: "servicePricingOverrides" });
  };

  return Merchant;
};
