module.exports = function (sequelize, DataTypes) {
  const publish_events = sequelize.define(
    'publish_events',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      title: {
        type: DataTypes.STRING,
        allowNull: true,
        validate: {
          len: { args: [0, 255], msg: 'Title must be at most 255 characters' },
        },
      },

      description: {
        type: DataTypes.TEXT,
        allowNull: true,
        validate: {
          len: {
            args: [0, 5000],
            msg: 'Description must be at most 5000 characters',
          },
        },
      },

      from_environment: {
        type: DataTypes.ENUM,
        allowNull: false,

        values: ['dev', 'stage', 'production'],
      },

      to_environment: {
        type: DataTypes.ENUM,
        allowNull: false,

        values: ['dev', 'stage', 'production'],
      },

      started_at: {
        type: DataTypes.DATE,
      },

      finished_at: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.ENUM,
        allowNull: false,
        defaultValue: 'queued',

        values: ['queued', 'running', 'success', 'failed'],
      },

      error_message: {
        type: DataTypes.TEXT,
      },

      pages_copied: {
        type: DataTypes.INTEGER,
        validate: {
          min: {
            args: [0],
            msg: 'Pages copied must be a non-negative integer',
          },
        },
      },

      transitions_copied: {
        type: DataTypes.INTEGER,
        validate: {
          min: {
            args: [0],
            msg: 'Transitions copied must be a non-negative integer',
          },
        },
      },

      audios_copied: {
        type: DataTypes.INTEGER,
        validate: {
          min: {
            args: [0],
            msg: 'Audios copied must be a non-negative integer',
          },
        },
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
      indexes: [
        { fields: ['projectId'] },
        { fields: ['userId'] },
        { fields: ['status'] },
        { fields: ['started_at'] },
      ],
    },
  );

  publish_events.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    //end loop

    db.publish_events.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.publish_events.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

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

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

  return publish_events;
};
