export default (sequelize, DataTypes) => {
  const KybDirector = sequelize.define("KybDirector", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    applicationId: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: "KybApplications", key: "id" },
    },
    firstName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: { isEmail: true },
    },
    phoneNumber: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    role: {
      type: DataTypes.ENUM("Director", "Beneficial Owner", "Other"),
      defaultValue: "Director",
      allowNull: false,
    },
    ownershipPercentage: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      comment: "Required for beneficial owners",
    },
    idType: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "e.g. Passport, National ID",
    },
    idNumber: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    idDocumentUrl: {
      type: DataTypes.STRING,
      allowNull: true,
      validate: { isUrl: true },
    },
    isBeneficialOwner: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      allowNull: false,
    },
    isDirector: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      allowNull: false,
    },
    bvn: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    gender: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    dateOfBirth: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    residentialAddress: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    proofOfAddressUrl: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  }, {
    tableName: "KybDirectors",
    timestamps: true,
  });

  KybDirector.associate = (models) => {
    KybDirector.belongsTo(models.KybApplication, {
      foreignKey: "applicationId",
      as: "application",
    });
  };

  return KybDirector;
};
