const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const UserChild = sequelize.define('userchild', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        child_name: { type: DataTypes.STRING(100), allowNull: false },
        child_age: { type: DataTypes.INTEGER, allowNull: false },
        child_age_range: { type: DataTypes.STRING(100), allowNull: false },

        // fk
        user_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'Users',
                key: 'id'
            }
        },
    }, {
        tableName: 'userchilds',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the UserChild model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A UserChild belongs to one User.
     */
    UserChild.associate = (models) => {
        /**
         * Associates the UserChild model with the User model.
         * @param {object} models - The models to associate with.
         * @description
         *   - A UserChild belongs to one User.
         */
        UserChild.belongsTo(models.User, { foreignKey: 'user_id', as: 'user' });
    };

    return UserChild;
};
