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

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 = new Scanner(System.in);

    public void placeOrder(int userId) {

        Order order = new Order();
        order.setUserId(userId);
        order.setCreatedAt(LocalDateTime.now());

        double total = 0;

        int orderId = orderDao.save(order);

        if (orderId == -1) {
            System.out.println("Failed to create order!");
            return;
        }

        while (true) {

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

            if (itemId == 0) break;

            MenuItem item = menuItemDao.findById(itemId);

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

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

            double itemTotal = item.getPrice() * qty;
            total += itemTotal;

            OrderDetail detail = new OrderDetail();
            detail.setOrderId(orderId);
            detail.setMenuItemId(itemId);
            detail.setQuantity(qty);
            detail.setPrice(item.getPrice());

            orderDetailDao.save(detail);

            System.out.println("Added: " + item.getName());
        }

        order.setTotalPrice(total);
        orderDao.updateTotalPrice(orderId, total);

        System.out.println("Order placed successfully! Total: " + total);
    }

    public void printReceipt(int orderId) {

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

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

        double total = 0;

        for (OrderDetail d : details) {

            MenuItem item = menuItemDao.findById((int) d.getMenuItemId());

            double lineTotal = d.getPrice() * d.getQuantity();
            total += lineTotal;

            System.out.println(
                    item.getName() + " x " +
                            d.getQuantity() + " = " +
                            lineTotal
            );
        }

        System.out.println("-------------------");
        System.out.println("TOTAL: " + total);
        System.out.println("===================");
    }

    public void showOrderHistory(int userId) {

        List<Order> orders = orderDao.findByUserId(userId);

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

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

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