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.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class OrderService {

    private final Scanner scanner = new Scanner(System.in);
    private final MenuItemDao menuDao = new MenuItemDao();
    private final OrderDao orderDao = new OrderDao();
    private final OrderDetailDao detailDao = new OrderDetailDao();

    public void placeOrder(int userId)
    {
        List<OrderDetail> details = new ArrayList<>();

        double totalPrice = 0;

        while (true)
        {
            System.out.println("\nEnter menu item id (0 to finish): ");

            int itemId = scanner.nextInt();

            if (itemId == 0)  break;

            MenuItem item = menuDao.findById(itemId);

            if (item == null)
            {
                System.out.println("Item not found.");
                continue;
            }

            System.out.println("Quantity: ");

            int quantity = scanner.nextInt();

            OrderDetail detail = new OrderDetail();

            detail.setMenuItemId(itemId);
            detail.setQuantity(quantity);
            detail.setPrice(item.getPrice());

            totalPrice += item.getPrice() * quantity;

            details.add(detail);
        }

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

        Order order = new Order();

        order.setUserId(userId);
        order.setTotalPrice(totalPrice);

        int orderId = orderDao.save(order);

        for (OrderDetail detail : details)
        {
            detail.setOrderId(orderId);

            detailDao.save(detail);
        }

        printReceipt(orderId);
        System.out.println("Order saved successfully.");
    }

    public void printReceipt(int orderId)
    {
        List<OrderDetail> details = detailDao.findByOrderId(orderId);

        System.out.println("\n[Order Summary / Receipt]");
        System.out.println("----------------------------------------------------");
        System.out.printf("%-15s %-8s %-10s %-10s%n",
                "Item", "Qty", "Unit", "Total");
        System.out.println("----------------------------------------------------");

        double finalTotal = 0;

        for(OrderDetail detail : details)
        {
            MenuItem item = menuDao.findById(detail.getMenuItemId());

            double subtotal = detail.getPrice() * detail.getQuantity();

            finalTotal += subtotal;

            System.out.printf("%-15s %-8d $%-9.2f $%-9.2f%n",
                    item.getName(),
                    detail.getQuantity(),
                    detail.getPrice(),
                    subtotal);
        }

        System.out.println("----------------------------------------------------");
        System.out.printf("Final Total: $%.2f%n", finalTotal);
    }

    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 order : orders)
        {
            System.out.println("Order ID: " + order.getId()
                    + " | Total: $" + order.getTotalPrice()
                    + " | Date: " + order.getCreatedAt()
            );
        }
    }
}