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 menuItemDao = new MenuItemDao();
    private final OrderDao orderDao = new OrderDao();
    private final OrderDetailDao orderDetailDao = new OrderDetailDao();

    public void placeOrder(int userId) {

        List<OrderDetail> cart = new ArrayList<>();
        double totalPrice = 0;

        while (true) {

            System.out.print("Enter item id or 0 to finish: ");
            int itemId = scanner.nextInt();

            if (itemId == 0) {
                break;
            }

            MenuItem menuItem = menuItemDao.findById(itemId);

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

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

            if (quantity <= 0) {
                System.out.println("Quantity must be greater than 0.");
                continue;
            }

            OrderDetail detail = new OrderDetail();
            detail.setMenuItemId(menuItem.getId());
            detail.setQuantity(quantity);
            detail.setPrice(menuItem.getPrice());

            cart.add(detail);

            totalPrice += menuItem.getPrice() * quantity;

            System.out.println("Added " + quantity + "x " + menuItem.getName());
        }

        if (cart.isEmpty()) {
            System.out.println("Order cancelled.");
            return;
        }

        Order order = new Order();
        order.setUserId(userId);
        order.setTotalPrice(totalPrice);

        int orderId = orderDao.save(order);

        if (orderId == -1) {
            System.out.println("Order could not be saved.");
            return;
        }

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

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

    public void printReceipt(int orderId) {

        List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);

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

        double finalTotal = 0;

        System.out.println();
        System.out.println("===== RECEIPT =====");

        for (OrderDetail detail : details) {

            MenuItem item = menuItemDao.findById(detail.getMenuItemId());

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

            System.out.println(
                    item.getName() +
                            " | Qty: " + detail.getQuantity() +
                            " | Unit: $" + detail.getPrice() +
                            " | Total: $" + subtotal
            );
        }

        System.out.println("-------------------");
        System.out.println("Final Total: $" + 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();
        System.out.println("===== ORDER HISTORY =====");

        for (Order order : orders) {
            System.out.println(
                    "Order #" + order.getId() +
                            " | Date: " + order.getCreatedAt() +
                            " | Total: $" + order.getTotalPrice()
            );
        }
    }
}