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

      environment: {
        type: DataTypes.ENUM,
        allowNull: false,
        defaultValue: 'dev',

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

      source_key: {
        type: DataTypes.TEXT,
      },

      name: {
        type: DataTypes.TEXT,
        allowNull: false,
        validate: {
          notEmpty: { msg: 'Page name is required' },
          len: {
            args: [1, 255],
            msg: 'Page name must be between 1 and 255 characters',
          },
        },
      },

      slug: {
        type: DataTypes.TEXT,
        allowNull: false,
        validate: {
          notEmpty: { msg: 'Slug is required' },
          is: {
            args: /^[a-z0-9_-]+$/i,
            msg: 'Slug can only contain letters, numbers, dashes, and underscores',
          },
          len: {
            args: [1, 255],
            msg: 'Slug must be between 1 and 255 characters',
          },
        },
      },

      sort_order: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },

      background_image_url: {
        type: DataTypes.TEXT,
      },

      background_video_url: {
        type: DataTypes.TEXT,
      },

      background_audio_url: {
        type: DataTypes.TEXT,
      },

      background_loop: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      requires_auth: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      ui_schema_json: {
        type: DataTypes.JSON,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
      indexes: [
        { fields: ['projectId'] },
        { fields: ['projectId', 'environment', 'slug'], unique: true },
        { fields: ['projectId', 'environment', 'sort_order'] },
        { fields: ['deletedAt'] },
      ],
    },
  );

  tour_pages.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.tour_pages.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

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

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

  return tour_pages;
};
