package com.nisum.servlet;

import com.nisum.dao.CartDAO;
import com.nisum.model.CartItem;
import com.nisum.model.ShoppingCart;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class OrderServlet extends HttpServlet {

    private CartDAO cartDAO = new CartDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        int userId = 1; // Replace with session-based value later

        try {
            ShoppingCart cart = cartDAO.getOrCreateCart(userId);
            List<CartItem> items = cartDAO.getCartItems(userId);

            req.setAttribute("cartItems", items);
            req.setAttribute("cartTotal", cart.getCartTotal().doubleValue());

            req.getRequestDispatcher("/jsp/order.jsp").forward(req, resp);
        } catch (SQLException e) {
            throw new ServletException("Unable to fetch order summary", e);
        }
    }
}
