const dbSAPPS = require("../../helpers/db");
require("dotenv").config();
const bcrypt = require("bcryptjs");
const axios = require("axios");
const { Op } = require("sequelize");

module.exports = {
	getWarehouseList,
	getOptionWarehouses,
};

async function getWarehouseList(params) {
	try {
		// get
		const warehouses = await axios.get(
			`${params.api_url}api/resource/Warehouse`,
			{
				headers: {
					Authorization: `token ${params.api_key}:${params.api_secret}`,
					"Content-Type":
						"application/x-www-form-urlencoded, multipart/form-data",
					Accept: "application/json",
				},
				params: {
					fields: '["*"]',
					limit_start: "0",
					limit_page_length: "0",
					order_by: "modified DESC",
					debug: "true",
					filters: '[["disabled","=",0]]',
				},
			}
		);

		return {
			warehouses: warehouses.data.data || [],
			success: true,
			message: "Loaded successfully!",
		};
	} catch (error) {
		const errorMessage = error?.message || "Failed to load, please try again!";
		return {
			success: false,
			message: errorMessage,
		};
	}
}

async function getOptionWarehouses(params) {
	try {
		// get
		const options = await axios.get(`${params.api_url}api/resource/Warehouse`, {
			headers: {
				Authorization: `token ${params.api_key}:${params.api_secret}`,
				"Content-Type":
					"application/x-www-form-urlencoded, multipart/form-data",
				Accept: "application/json",
			},
			params: {
				fields: '["*"]',
				limit_start: "0",
				limit_page_length: "0",
				order_by: "modified DESC",
				debug: "true",
                filters: '[["disabled","=",0]]',
			},
		});

		return {
			options: options.data.data || [],
			success: true,
			message: "Loaded successfully!",
		};
	} catch (error) {
		const errorMessage = error?.message || "Failed to load, please try again!";
		return {
			success: false,
			message: errorMessage,
		};
	}
}
