package com.reallinkbilling.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import org.apache.struts2.dispatcher.SessionMap;

import com.reallinkbilling.db.GetDBConnection;

public class GetProductDetailsById {

	public static HashMap<String, String> getProductDetails(
			String productId,
			SessionMap<String, Object> sessionMap) {

		HashMap<String, String> productDetails = new HashMap<>();

		String shopId = (String) sessionMap.get("shopId");
		String shopCode = (String) sessionMap.get("shopCode");
		String db = (String) sessionMap.get("db");

		if (shopId == null || shopCode == null || db == null) {
			throw new IllegalArgumentException(
					"Missing required session values (shopId, shopCode, or db).");
		}

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {

			connection = GetDBConnection.getDBConnection(db);

			if (connection == null) {
				throw new SQLException("Database connection is null");
			}

			String sql = "SELECT "
					+ "product_short_code, "
					+ "product_alias, "
					+ "sales_unit, "
					+ "purchase_unit, "
					+ "in_stock_quantity, "
					+ "tax_id "
					+ "FROM shop_product_master "
					+ "WHERE shop_id = ? "
					+ "AND shop_code = ? "
					+ "AND product_id = ?";

			preparedStatement = connection.prepareStatement(sql);

			preparedStatement.setInt(1, Integer.parseInt(shopId));
			preparedStatement.setString(2, shopCode);
			preparedStatement.setString(3, productId);

			resultSet = preparedStatement.executeQuery();

			if (resultSet.next()) {

				productDetails.put(
						"PRODUCTSHORTCODE",
						resultSet.getString("product_short_code"));

				productDetails.put(
						"PRODUCTALIAS",
						resultSet.getString("product_alias"));

				productDetails.put(
						"CONSUMPTIONUNIT",
						resultSet.getString("sales_unit"));

				productDetails.put(
						"PURCHASEUNIT",
						resultSet.getString("purchase_unit"));

				productDetails.put(
						"INSTOCKQTY",
						resultSet.getString("in_stock_quantity"));

				productDetails.put(
						"TAXID",
						resultSet.getString("tax_id"));
			}

		} catch (SQLException e) {

			// System.out.println("SQLException occurred in GetProductDetailsById:");
			// e.printStackTrace();

		} catch (Exception e) {

			// System.out.println("Unexpected exception in GetProductDetailsById:");
			// e.printStackTrace();

		} finally {

			// Close ResultSet
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					// e.printStackTrace();
				}
			}

			// Close PreparedStatement
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// e.printStackTrace();
				}
			}

			// Close Connection
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// e.printStackTrace();
				}
			}
		}

		return productDetails;
	}
}

