const Joi = require("joi");
const { Validate } = require("./Validate");
const { Reports } = require("../utils/Constants");

const AddPropertyValidation = async (req, res, next) => {
  const schema = Joi.object().keys({
    name: Joi.string().required().messages({
      "any:required": "Name field is required",
    }),
    id: Joi.number().integer().messages({
      "any:required": "Id field is required",
    }),
    addressId: Joi.number().integer().messages({
      "any:required": "Address Id field is required",
    }),
    type: Joi.string()
      .required()
      .valid("buildingFloor","flat","buildingfloor", "office", "plotorland","plotOrLand", "vila", "apartment")
      .messages({
        "any:required": "Type field is required",
      }),
    status: Joi.string().required().valid("rent", "buy", "sell").messages({
      "any:required": "Status field is required",
    }),
    price: Joi.number().integer().required().messages({
      "any:required": "Price field is required",
    }),
    description: Joi.string().required().messages({
      "any:required": "Description field is required",
    }),
    furnishing: Joi.string()
      .valid("furnished", "semifurnished", "unfurnished", "", null)
      .messages({
        "any:required": "Furnishing field is required",
      }),
    street: Joi.string().required().messages({
      "any:required": "Street field is required",
    }),
    city: Joi.string().required().messages({
      "any:required": "City field is required",
    }),
    state: Joi.string().required().messages({
      "any:required": "State field is required",
    }),
    postalCode: Joi.string().required().messages({
      "any:required": "Postal Code field is required",
    }),
    latitude: Joi.number().integer().optional().allow(null, ""),
    longitude: Joi.number().integer().optional().allow(null, ""),
    propertyFeatures: Joi.string().optional().allow(null, ""),
    image: Joi.string().optional().allow("", null),
    propertyKind: Joi.string().optional().allow("", null),
    listedBy: Joi.string().optional().allow(null, ""),
    propertyExtraFlags: Joi.string().optional().allow(null, ""),
    reraNumber: Joi.string().optional().allow(null, ""),
    noOfRooms: Joi.number().integer().optional().allow(null, 0),
    noOfBathRooms: Joi.number().integer().optional().allow(null, 0),
    nearbyFecilities: Joi.string().optional().allow(null, ""),
    area: Joi.any().optional().allow(null, "0"),
    totalFloors: Joi.number().integer().optional().allow(null, 0),
    propertyOnFloor: Joi.number().integer().optional().allow(null, 0),
  });
  await Validate(req, res, next, schema);
};

const BookmarkPropertyValidation = async (req, res, next) => {
  const schema = Joi.object().keys({
    propertyId: Joi.number().integer().required().messages({
      "any:required": "PropertyId field is required",
    }),
  });
  await Validate(req, res, next, schema);
};

const PayOnAccessValidation = async (req, res, next) => {
  const schema = Joi.object().keys({
    propertyId: Joi.number().integer().required().messages({
      "any:required": "PropertyId field is required",
    }),
  });
  await Validate(req, res, next, schema);
};

const ReportValidation = async (req, res, next) => {
  const schema = Joi.object().keys({
    propertyId: Joi.number().integer().required().messages({
      "any:required": "PropertyId field is required",
    }),
    reportType: Joi.string()
      .required()
      .valid(...Reports.map((e) => e.id))
      .messages({
        "any:required": "Report Type field is required",
      }),
    description: Joi.string().allow("", null).optional().messages({
      "any:required": "Description field is required",
    }),
  });
  await Validate(req, res, next, schema);
};

module.exports = {
  AddPropertyValidation,
  BookmarkPropertyValidation,
  PayOnAccessValidation,
  ReportValidation,
};

