"use strict";

module.exports = (sequalize, DataTypes) => {
  const PriceHistory = sequalize.define(
    "PriceHistory",
    {
      id: {
        type: DataTypes.BIGINT,
        autoIncrement: true,
        primaryKey: true,
        allowNull: false
      },
      realEstateId: {
        type: DataTypes.BIGINT,
        allowNull: false,
        unique: "uniquePriceRealEstate",
        references: {
          model: "RealEstates",
          key: "id"
        },
        onUpdate: "CASCADE",
        onDelete: "SET NULL"
      },
      price: {
        type: DataTypes.REAL,
        unique: "uniquePriceRealEstate"
      }
    },
    {
      freezeTableName: true
    }
  );

  PriceHistory.associate = models => {
    PriceHistory.hasMany(models.RealEstate, {
      foreignKey: "id",
      sourceKey: "realEstateId",
      targetKey: "id",
      as: "realEstates"
    });
  };

  return PriceHistory;
};
