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;
    private final OrderDetailDao orderDetailDao;
    private final MenuItemDao menuItemDao;

    public OrderService(OrderDao orderDao,
                        OrderDetailDao orderDetailDao,
                        MenuItemDao menuItemDao) {
        this.orderDao = orderDao;
        this.orderDetailDao = orderDetailDao;
        this.menuItemDao = menuItemDao;
    }

    public void placeOrder(int userId)
    {
        Scanner scanner = new Scanner(System.in);

        double totalPrice = 0;

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

        int orderId = orderDao.save(order);

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

        System.out.println("Available Menu:");
        List<MenuItem> items = menuItemDao.findAll();

        for (MenuItem item : items)
        {
            System.out.println(item.getId() + ". " +
                    item.getName() + " - $" +
                    item.getPrice());
        }

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

            if (itemId == 0) break;

            MenuItem menuItem = menuItemDao.findById(itemId);

            if (menuItem == null)
            {
                System.out.println("Invalid item!");
                continue;
            }

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

            double itemTotal = menuItem.getPrice() * qty;
            totalPrice += itemTotal;

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

            orderDetailDao.save(detail);

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

        System.out.println("=================================");
        System.out.println("Final Total: $" + totalPrice);
        System.out.println("Order placed successfully!");
    }

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

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

        System.out.println("=================================");
        System.out.println("           RECEIPT");
        System.out.println("Order ID: " + orderId);
        System.out.println("=================================");
        System.out.println("Item\tQty\tPrice\tTotal");

        double grandTotal = 0;

        for (OrderDetail d : details)
        {
            MenuItem item = menuItemDao.findById(d.getMenuItemId());

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

            System.out.println(item.getName() + "\t"
                    + d.getQuantity() + "\t"
                    + d.getPrice() + "\t"
                    + total);
        }

        System.out.println("=================================");
        System.out.println("Grand Total: $" + grandTotal);
        System.out.println("=================================");
    }

    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");
        System.out.println("=================================");

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