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

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

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

        values: [

"dev",


"stage",


"production"

        ],

      },

logo_url: {
        type: DataTypes.TEXT,
      
      

      },

favicon_url: {
        type: DataTypes.TEXT,
      
      

      },

og_image_url: {
        type: DataTypes.TEXT,
      
      

      },

theme_config_json: {
        type: DataTypes.TEXT,
      
      

      },

custom_css_json: {
        type: DataTypes.TEXT,
      
      

      },

cdn_base_url: {
        type: DataTypes.TEXT,
      
      

      },

entry_page_slug: {
        type: DataTypes.TEXT,
      
      

      },

is_deleted: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

deleted_at_time: {
        type: DataTypes.DATE,
      
      

      },

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

  projects.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity






    db.projects.hasMany(db.project_memberships, {
      as: 'project_memberships_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.assets, {
      as: 'assets_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });



    db.projects.hasMany(db.presigned_url_requests, {
      as: 'presigned_url_requests_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


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




    db.projects.hasMany(db.transitions, {
      as: 'transitions_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.project_audio_tracks, {
      as: 'project_audio_tracks_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.publish_events, {
      as: 'publish_events_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.pwa_caches, {
      as: 'pwa_caches_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });


    db.projects.hasMany(db.access_logs, {
      as: 'access_logs_project',
      foreignKey: {
          name: 'projectId',
      },
      constraints: false,
    });



//end loop






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

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



  return projects;
};

