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

const Property = db.define("properties", {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },

  userId: {
    type: DataTypes.INTEGER,
    references: {
      model: "users",
      key: "id",
    },
    allowNull: true,
  },

  name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  propertyKind: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  type: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  status: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  videoLink: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
  propertyExtraFlags: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
  furnishing: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: "",
  },
  addressId: {
    type: DataTypes.INTEGER,
    references: {
      model: "addresses",
      key: "id",
    },
    allowNull: true,
  },
  price: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  reraNumber: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  totalPrice: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  noOfRooms: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  noOfBathRooms: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  image: {
    type: DataTypes.TEXT,
    allowNull: false,
  },
  propertyStatus: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  payOnAccess: {
    type: DataTypes.FLOAT,
    allowNull: false,
    defaultValue: 0,
  },
  featured: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: false,
  },
  nearbyFecilities: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
  area: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: "0",
  },
  totalFloors: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  propertyOnFloor: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  listedBy: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: "owner",
  },
  brochureUrl: {
    type: DataTypes.STRING,
    allowNull: true
  },
});

Address.hasOne(Property, { foreignKey: "addressId" });
Property.belongsTo(Address, { foreignKey: "addressId" });

User.hasOne(Property, { foreignKey: "userId" });
Property.belongsTo(User, { foreignKey: "userId" });

module.exports = Property;
