module.exports = (sequelize, DataTypes) => {
  const State = sequelize.define('State', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    country_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Country',
        key: 'id'
      }
    },
    state_code: {
      type: DataTypes.STRING(10),
      allowNull: false
    },
    state_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    gst_state_code: {
      type: DataTypes.STRING(2),
      allowNull: true,
      comment: 'GST state code (1-38 for Indian states/UTs)'
    },
    is_union_territory: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'True for Union Territories'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    }
  }, {
    tableName: 'state',
    indexes: [
      {
        unique: true,
        fields: ['country_id', 'state_code']
      },
      {
        fields: ['gst_state_code']
      },
      {
        fields: ['is_active']
      }
    ]
  });

  State.associate = function(models) {
    State.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
    State.hasMany(models.City, { foreignKey: 'state_id', as: 'cities' });
  };

  return State;
};

