package com.nisum.servlet;

import com.nisum.dao.AddressDAO;
import com.nisum.dao.CartDAO;
import com.nisum.model.CartItem;
import com.nisum.model.ShoppingCart;
import com.nisum.model.UserAddress;
import com.nisum.dao.OrderDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;


public class CheckoutServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userIdStr = request.getParameter("userId");
        String cartIdStr = request.getParameter("cartId");
        String addressIdStr = request.getParameter("addressId");

        try {
            int userId = Integer.parseInt(userIdStr);
            int cartId = Integer.parseInt(cartIdStr);
            int addressId = Integer.parseInt(addressIdStr);

            AddressDAO addressDAO = new AddressDAO();
            UserAddress userAddress = addressDAO.getById(addressId);

            if (userAddress == null) {
                // Handle missing address scenario
                response.sendRedirect("error.jsp");
                return;
            }

            // Set attributes
            request.setAttribute("userId", userId);
            request.setAttribute("cartId", cartId);
            request.setAttribute("addressId", addressId);
            request.setAttribute("userAddress", userAddress); // Pass the address object

            request.getRequestDispatcher("/jsp/checkout.jsp").forward(request, response);

        } catch (NumberFormatException | SQLException e) {
            e.printStackTrace();
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    }

}


