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

module.exports = (sequelize) => {
  const Worship = sequelize.define('Worship', {
    eventPlaceId: {
      type: DataTypes.INTEGER,
      references: {
        model: 'EventPlace',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    },
    date: {
      type: DataTypes.DATE,
      allowNull: false
    },
    time: {
      type: DataTypes.TIME,
      allowNull: false
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    organizer: {
      type: DataTypes.STRING,
      allowNull: true
    },
    collection: {
      type: DataTypes.STRING,
      allowNull: true
    },
    address: {
      type: DataTypes.STRING,
      allowNull: true
    },
    selfInformation: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    highlightTime: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    neighborInvitation: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    introLine: {
      type: DataTypes.STRING,
      allowNull: true
    },
    dayName: {
      type: DataTypes.STRING,
      defaultValue: '',
      allowNull: false
    },
    sacristanService: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'sacristan_service'
    },
    organPlaying: {
      type: DataTypes.STRING,
      allowNull: true,
      field: 'organ_playing'
    },
    approved: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      allowNull: false,
      field: 'approved'
    },
  }, {
    tableName: 'worships',
    timestamps: true
  });

  Worship.associate = function(models) {
    Worship.belongsTo(models.EventPlace, {
      foreignKey: 'eventPlaceId',
      as: 'eventPlace'
    });
  };

  return Worship;
};
