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,
        validate: {
          notEmpty: { msg: 'Project name is required' },
          len: {
            args: [1, 255],
            msg: 'Project name must be between 1 and 255 characters',
          },
        },
      },

      slug: {
        type: DataTypes.TEXT,
        allowNull: false,
        unique: true,
        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',
          },
        },
      },

      description: {
        type: DataTypes.TEXT,
      },

      logo_url: {
        type: DataTypes.TEXT,
      },

      favicon_url: {
        type: DataTypes.TEXT,
      },

      og_image_url: {
        type: DataTypes.TEXT,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
      indexes: [{ fields: ['slug'], unique: true }, { 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: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.assets, {
      as: 'assets_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.presigned_url_requests, {
      as: 'presigned_url_requests_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.tour_pages, {
      as: 'tour_pages_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.project_audio_tracks, {
      as: 'project_audio_tracks_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

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

    db.projects.hasMany(db.pwa_caches, {
      as: 'pwa_caches_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.access_logs, {
      as: 'access_logs_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    db.projects.hasMany(db.project_element_defaults, {
      as: 'project_element_defaults_project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    });

    //end loop

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

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

  return projects;
};
