module.exports = function(sequelize, DataTypes) {
  const vorta_commerce_states = sequelize.define(
    'vorta_commerce_states',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      settings: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: {},
      },
      campaigns: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: [],
      },
      ledger: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: [],
      },
      product_meta: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: {},
      },
      customer_meta: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: {},
      },
      order_meta: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: {},
      },
      shipment_meta: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: {},
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  vorta_commerce_states.associate = (db) => {
    db.vorta_commerce_states.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.vorta_commerce_states.belongsTo(db.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.vorta_commerce_states.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });

    db.vorta_commerce_states.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.vorta_commerce_states.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return vorta_commerce_states;
};
