package dev.service;
import dev.dao.MenuItemDao;
import dev.dao.OrderDao;
import dev.dao.OrderDetailDao;
import dev.model.MenuItem;
import dev.model.Order;
import dev.model.OrderDetail;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
public class OrderService {
    private final OrderDao orderDao = new OrderDao();
    private final OrderDetailDao orderDetailDao = new OrderDetailDao();
    private final MenuItemDao menuItemDao = new MenuItemDao();

    public int placeOrder(int userId, Map<Integer, Integer> itemQuantities) {

        // TODO:
        // Create order

        if (itemQuantities == null || itemQuantities.isEmpty()) {
            return -1;
        }

        BigDecimal total = BigDecimal.ZERO;

        for (Map.Entry<Integer, Integer> entry : itemQuantities.entrySet()) {
            MenuItem item = menuItemDao.findById(entry.getKey());
            if (item == null) {
                continue;
            }
            int quantity = entry.getValue();
            total = total.add(item.getPrice().multiply(BigDecimal.valueOf(quantity)));
        }

        Order order = new Order(userId, total);
        int orderId = orderDao.save(order);

        if (orderId == -1) {
            return -1;
        }

        for (Map.Entry<Integer, Integer> entry : itemQuantities.entrySet()) {
            MenuItem item = menuItemDao.findById(entry.getKey());
            if (item == null) {
                continue;
            }
            int quantity = entry.getValue();

            OrderDetail detail = new OrderDetail(orderId, item.getId(), quantity, item.getPrice());
            orderDetailDao.save(detail);
        }

        return orderId;
    }

    public void printReceipt(int orderId) {

        // TODO:
        // Print order receipt
        List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);

        if (details.isEmpty()) {
            System.out.println("No details found for order #" + orderId);
            return;
        }

        System.out.println("---------------------------------------");
        System.out.printf("%-12s %-6s %-10s %-10s%n", "Item", "Qty", "Unit", "Total");
        System.out.println("---------------------------------------");

        BigDecimal grandTotal = BigDecimal.ZERO;

        for (OrderDetail detail : details) {
            MenuItem item = menuItemDao.findById(detail.getMenuItemId());
            String name = (item != null) ? item.getName() : "Unknown item";
            BigDecimal subtotal = detail.getSubtotal();
            grandTotal = grandTotal.add(subtotal);

            System.out.printf("%-12s %-6d $%-9.2f $%-9.2f%n",
                    name, detail.getQuantity(), detail.getPrice(), subtotal);
        }

        System.out.println("---------------------------------------");
        System.out.println("Final Total: $" + grandTotal);
    }

    public void showOrderHistory(int userId) {

        // TODO:
        // Display user's order history
        List<Order> orders = orderDao.findByUserId(userId);

        if (orders.isEmpty()) {
            System.out.println("No past orders found.");
            return;
        }

        System.out.println("=======================================");
        System.out.println("          ORDER HISTORY");
        System.out.println("=======================================");

        for (Order order : orders) {
            System.out.println(order);
        }
    }
}