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

const Transaction = db.define("transactions", {
  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,
  },
  transactionId: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  razorPayPaymentId: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  orderId: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  paymentType: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: PaymentTypes.PAYONACCESS,
  },
  amount: {
    type: DataTypes.FLOAT,
    allowNull: false,
  },
  status: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: PaymentStatus.PENDING,
  },
  reason: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
});

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

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

module.exports = Transaction;
