const Sequelize = require("sequelize");

module.exports = function (sequelize, DataTypes) {
  const Location = sequelize.define(
    "Location",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      country: {
        type: DataTypes.STRING(125),
        allowNull: false,
      },
      city: {
        type: DataTypes.STRING(125),
        allowNull: false,
      },
      description: {
        type: DataTypes.STRING,
      },
      country_code: {
        type: DataTypes.STRING(2),
        default: "ES",
      },
    },
    {
      tableName: "locations",
      freezeTableName: true,
      timestamps: true,
      indexes: [
        {
          unique: true,
          fields: ["country", "city"],
        },
        {
          unique: false,
          fields: ["country_code"],
        },
      ],
    }
  );

  Location.associate = function (models) {
    Location.Events = Location.hasMany(models.Event, { as: "events", foreignKey: "locationId" });

    //OJO antes de force comentar
    // OJO GENERA UN FOREIGN KEY Con eventos y habrá ID de otras entidades que no exitan en la tabla eventos, porque son post o speakers
    Location.Multimedias = Location.hasMany(models.Multimedia, {
      foreignKey: "entityId",
      as: { singular: "multimedia", plural: "multimedias" },
    });
  };

  Location.addScope("includeMultimedias", () => {
    return {
      include: [
        {
          model: sequelize.models.Multimedia,
          as: { singular: "multimedia", plural: "multimedias" },
          required: false,
          include: [
            {
              model: sequelize.models.MultimediaFile,
              as: "multimediaFile",
            },
          ],
        },
      ],
    };
  });

  Location.addScope("includeEvents", () => {
    return {
      include: [
        {
          model: sequelize.models.Event,
          as: "events",
          required: false,
          //                group: ['typeId'],
          //                attributes: ['name', 'type.title',] //[sequelize.fn('COUNT', sequelize.col('events.typeId')), 'ediciones'],] // [sequelize.fn('SUM', sequelize.col('Event.assistants')), 'assistants']]
          //                attributes: [[sequelize.fn('COUNT', sequelize.col('typeId')), 'ediciones'],] // [sequelize.fn('SUM', sequelize.col('Event.assistants')), 'assistants']]
          //                include: [{
          //                    model: sequelize.models.MultimediaFile,
          //                    as: "multimediaFile"
          //                }]
        },
      ],
    };
  });

  Location.addScope("includeGroupEvents", () => {
    return {
      include: [
        {
          as: "events",
          required: false,
          //                include: [{
          //                    model: sequelize.models.MultimediaFile,
          //                    as: "multimediaFile"
          //                }]
        },
      ],
    };
  });

  return Location;
};
