const db = require("../models");
const Resource = db.resource;
const { splitSite } = require("../middlewares");
// Create and Save a new Resource
exports.createResource = (req, res) => {
  // Validate request
  if (!req.body.name) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  const resource = new Resource({
    // Basic Information
    name: req.body.name, // Provider name
    office_name: req.body.office_name || '',
    specialty: req.body.specialty,
    type: req.body.type, // value may be ['doctor', 'pharmacy' or 'other']
    type_other: req.body.type_other || '',
    
    // Legacy fields for backward compatibility
    name_original: req.body.name_original || req.body.office_name || '',
    name_branch: req.body.name_branch || '',
    
    // Contact Information
    phone: req.body.phone, // Office Phone Number
    contact: req.body.contact, // Secondary Phone Number
    fax: req.body.fax,
    email: req.body.email,
    
    // Address (split fields)
    address_line_1: req.body.address_line_1 || '',
    address_line_2: req.body.address_line_2 || '',
    city: req.body.city,
    state: req.body.state,
    zipcode: req.body.zipcode,
    
    // Legacy address field
    address: req.body.address || req.body.address_line_1 || '',
    
    // Additional Information
    note: req.body.note,
    
    // Legacy fields
    description: req.body.description || '',
    color: req.body.color || '',
    category: req.body.category,
    
    // System fields
    status: req.body.status || 'active',
    create_by: req.body.create_by,
    create_date: req.body.create_date || new Date(),
    parent_id: req.body.parent_id,
    ext_id: req.body.ext_id,
    data: req.body.data,
    edit_by: req.body.edit_by,
    edit_date: req.body.edit_date || new Date(),
    images: req.body.images,
    edit_history: req.body.edit_history,
    site
  });
  // Save Resource in the database
  resource
    .save(resource)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Resource"
      });
    });
};
// Retrieve all Resources from the database.
exports.getAllResources = (req, res) => {
  var params = req.query;
  var condition = {};
  if (params) {
    if (params.type) {
      condition.type = params.type;
    }
  }
  Resource.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving resources."
      });
    });
};
// Get One Resource by Id
exports.getResource = (req, res) => {
  const id = req.params.id;
  Resource.findById(id)
    .then(data => {
      if (!data)
        res.status(404).send({ message: "Not found Resource with id " + id });
      else res.send(data);
    })
    .catch(err => {
      res
        .status(500)
        .send({ message: "Error retrieving Resource with id=" + id });
    });
};

// Update a Resource by the id in the request
exports.updateResource = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  Resource.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Resource with id=${id}. Maybe Resource was not found!`
        });
      } else res.send({ success: true, message: "Resource was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Resource with id=" + id
      });
    });
};
// Disable an Resource by the id in the request
exports.disableResource = (req, res) => {
  const id = req.params.id;
  Resource.findByIdAndUpdate(id, { ...req.body, status: 'inactive'}, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Resource with id=${id}. Maybe Resource was not found!`
        });
      } else res.send({ success: true, message: "Resource was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Resource with id=" + id
      });
    });
};

// Delete a Resource by id
exports.deleteResource= (req, res) => {
  const id = req.params.id;
  Resource.findByIdAndRemove(id)
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot delete Resource with id=${id}. Maybe Resource was not found!`
        });
      } else {
        res.send({
          message: "Resource was deleted successfully!"
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Could not delete Resource with id=" + id
      });
    });
};