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,

      },

slug: {
        type: DataTypes.TEXT,
        allowNull: false,

      },

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.TEXT,
      
      

      },

      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











    db.tour_pages.hasMany(db.page_elements, {
      as: 'page_elements_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });


    db.tour_pages.hasMany(db.page_links, {
      as: 'page_links_from_page',
      foreignKey: {
          name: 'from_pageId',
      },
      constraints: false,
    });

    db.tour_pages.hasMany(db.page_links, {
      as: 'page_links_to_page',
      foreignKey: {
          name: 'to_pageId',
      },
      constraints: false,
    });








//end loop



    db.tour_pages.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });




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

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



  return tour_pages;
};

