module.exports = (sequelize, DataTypes) => {
  const City = sequelize.define('City', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    state_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'State',
        key: 'id'
      }
    },
    city_code: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    city_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    pincode_prefix: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Pincode prefix range (e.g., 400xxx)'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    }
  }, {
    tableName: 'city',
    indexes: [
      {
        fields: ['state_id']
      },
      {
        fields: ['city_code']
      },
      {
        fields: ['is_active']
      },
      {
        unique: true,
        fields: ['state_id', 'city_name']
      }
    ]
  });

  City.associate = function(models) {
    City.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
  };

  return City;
};

