package dev.service;

import dev.dao.MenuItemDao;
import dev.dao.OrderDao;
import dev.dao.OrderDetailDao;
import dev.model.*;
import java.util.*;

public class OrderService {
    private final OrderDao orderDao = new OrderDao();
    private final OrderDetailDao orderDetailDao = new OrderDetailDao();
    private final MenuItemDao menuItemDao = new MenuItemDao();
    private final Scanner scanner;

    public OrderService(Scanner scanner) {
        this.scanner = scanner;
    }

    public void placeOrder(int userId) {
        List<OrderDetail> cart = new ArrayList<>();
        double total = 0;

        while (true) {
            System.out.print("Enter menu item ID (0 to finish): ");
            int itemId = scanner.nextInt();
            scanner.nextLine();
            if (itemId == 0) break;

            MenuItem item = menuItemDao.findById(itemId);
            if (item == null) {
                System.out.println("Invalid item.");
                continue;
            }

            System.out.print("Enter quantity: ");
            int qty = scanner.nextInt();
            scanner.nextLine();

            OrderDetail detail = new OrderDetail();
            detail.setMenuItemId(itemId);
            detail.setQuantity(qty);
            detail.setPrice(item.getPrice());
            cart.add(detail);
            total += item.getPrice() * qty;
        }

        if (cart.isEmpty()) {
            System.out.println("No items selected.");
            return;
        }

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

        for (OrderDetail detail : cart) {
            detail.setOrderId(orderId);
            orderDetailDao.save(detail);
        }

        System.out.println("Order placed successfully! Order ID: " + orderId);
        printReceipt(orderId);
    }

    public void printReceipt(int orderId) {
        List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
        MenuItemDao menuDao = new MenuItemDao();

        System.out.println("\n===== RECEIPT =====");
        System.out.printf("%-20s %5s %8s %8s%n", "Item", "Qty", "Unit", "Total");
        System.out.println("------------------------------------------");

        double grandTotal = 0;
        for (OrderDetail d : details) {
            MenuItem item = menuDao.findById(d.getMenuItemId());
            String name = (item != null) ? item.getName() : "Unknown";
            double lineTotal = d.getQuantity() * d.getPrice();
            grandTotal += lineTotal;
            System.out.printf("%-20s %5d $%7.2f $%7.2f%n", name, d.getQuantity(), d.getPrice(), lineTotal);
        }
        System.out.println("------------------------------------------");
        System.out.printf("Total: $%.2f%n", grandTotal);
    }

    public void showOrderHistory(int userId) {
        List<Order> orders = orderDao.findByUserId(userId);
        if (orders.isEmpty()) {
            System.out.println("No orders found.");
            return;
        }
        System.out.println("\n===== ORDER HISTORY =====");
        for (Order o : orders) {
            System.out.printf("Order #%d | Date: %s | Total: $%.2f%n",
                    o.getId(), o.getCreatedAt().toLocalDate(), o.getTotalPrice());
        }
    }

}