const { DataTypes } = require("sequelize");
const { db } = require("../utils");
const User = require("./User");
const Property = require("./Property");

const PayOnAccess = db.define("payonaccesses", {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  userId: {
    type: DataTypes.INTEGER,
    references: {
      model: "users",
      key: "id",
    },
    allowNull: true,
  },
  propertyId: {
    type: DataTypes.INTEGER,
    references: {
      model: "properties",
      key: "id",
    },
    allowNull: true,
  },
  amount: {
    type: DataTypes.FLOAT,
    allowNull: false,
    defaultValue: 0,
  },
});

User.hasMany(PayOnAccess, { foreignKey: "userId" });
PayOnAccess.belongsTo(User, { foreignKey: "userId" });

Property.hasMany(PayOnAccess, { foreignKey: "propertyId" });
PayOnAccess.belongsTo(Property, { foreignKey: "propertyId" });

module.exports = PayOnAccess;
