module.exports = (sequelize, DataTypes) => {
  const Country = sequelize.define('Country', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    country_code: {
      type: DataTypes.STRING(3),
      allowNull: false,
      unique: true,
      comment: 'ISO 3166-1 alpha-3 country code'
    },
    country_code_iso2: {
      type: DataTypes.STRING(2),
      allowNull: true,
      unique: true,
      comment: 'ISO 3166-1 alpha-2 country code'
    },
    country_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    phone_code: {
      type: DataTypes.STRING(5),
      allowNull: true,
      comment: 'International dialing code'
    },
    currency_code: {
      type: DataTypes.STRING(3),
      allowNull: true,
      comment: 'Default currency code for the country'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    }
  }, {
    tableName: 'country',
    indexes: [
      {
        unique: true,
        fields: ['country_code']
      },
      {
        unique: true,
        fields: ['country_code_iso2']
      },
      {
        fields: ['is_active']
      }
    ]
  });

  Country.associate = function(models) {
    Country.hasMany(models.State, { foreignKey: 'country_id', as: 'states' });
  };

  return Country;
};

