"use strict";

module.exports = (sequelize, DataTypes) => {
  const SearchRequestMatch = sequelize.define(
    "SearchRequestMatch",
    {
      id: {
        type: DataTypes.BIGINT,
        autoIncrement: true,
        allowNull: false
      },
      realEstateId: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        references: {
          model: "RealEstate",
          key: "id"
        },
        onUpdate: "CASCADE",
        onDelete: "SET NULL"
      },
      searchRequestId: {
        type: DataTypes.UUID,
        allowNull: false,
        primaryKey: true,
        references: {
          model: "SearchRequest",
          key: "id"
        }
      },
      notified: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
      }
    },
    {
      name: {
        singular: "searchRequestMatch",
        plural: "searchRequestMatches"
      }
    }
  );

  SearchRequestMatch.associate = models => {
    SearchRequestMatch.hasMany(models.SearchRequest, {
      foreignKey: "id",
      sourceKey: "searchRequestId",
      targetKey: "id",
      as: "searchRequests"
    });
    SearchRequestMatch.hasMany(models.RealEstate, {
      foreignKey: "id",
      as: "realEstates"
    });
  };

  return SearchRequestMatch;
};
