const {
  getAllContactDb,
  getContactByIdDb,
  createContactDb,
  updateContactDb,
  deleteContactDb
} = require('../db/contact.db');
const { ErrorHandler } = require('../helpers/error');

class ContactService {
  // Get all Contact
  static async getAllContact(param) {
    try {
      const results = await getAllContactDb(param);

      results.data.map(element => {
      });

      return results
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Get Contact by ID
  static async getContactById(id) {
    try {
      const result = await getContactByIdDb(id);

      if (result.length < 1) throw new ErrorHandler(404, 'Contact not found');

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Create Contact
  static async createContact(data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const result = await createContactDb(data);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Update Contact
  static async updateContact(id, data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const dataExist = await getContactByIdDb(id);

      if (dataExist.length < 1) {
        throw new ErrorHandler(404, 'Contact not found');
      }

      const result = await updateContactDb(id, data);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Soft delete Contact
  static async deleteContact(id, userId) {
    try {
      const dataExist = await getContactByIdDb(id);

      if (dataExist.length < 1) {
        throw new ErrorHandler(404, 'Contact not found');
      }

      const result = await deleteContactDb(id, userId);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }
}

module.exports = ContactService;
