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 {

    MenuItemDao menuItemDao = new MenuItemDao();
    OrderDetailDao orderDetailDao = new OrderDetailDao();
    OrderDao orderDao = new OrderDao();

    Scanner in = new Scanner(System.in);

    public void placeOrder(int userId) {

        List<OrderDetail> details = new ArrayList<>();
        int totalPrice = 0;

        // Create order
        while (true) {
            System.out.println("Enter the id of your order (-1 to exit) : ");
            int id = in.nextInt();

            if(id==-1) break;

            MenuItem menuItem = menuItemDao.findById(id);

            if(menuItem != null) {

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

                if(quantity<=0) {
                    System.out.println("Quantity can't be negative");
                    continue;
                }

                OrderDetail orderDetail = new OrderDetail();

                orderDetail.setPrice(menuItem.getPrice());
                orderDetail.setQuantity(quantity);
                orderDetail.setMenuItemId(id);

                details.add(orderDetail);
                totalPrice += menuItem.getPrice()*quantity;

                System.out.println(quantity+ " " + menuItem.getName() + "(s) got added to your order.");
            }
        }

        if (totalPrice<=0) return;

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

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

        for(OrderDetail orderDetail : details) {
            orderDetail.setOrderId(orderId);
            orderDetailDao.save(orderDetail);
        }

        printReceipt(orderId);
    }

    public void printReceipt(int orderId) {

        // Print order receipt
        List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);

        if (details.isEmpty()) return;

        double totalPrice = 0;

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

        for (OrderDetail detail : details) {

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

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

            System.out.println( item.getName() + " | Quantity: " + detail.getQuantity() +
                            " | Price: " + detail.getPrice() + "$ | Total: " + subtotal + "$");
        }
        System.out.println("==============================");
        System.out.println("Final Total: $" + totalPrice);
    }

    public void showOrderHistory(int userId) {

        // Display user's order history
        List<Order> orders = orderDao.findByUserId(userId);

        if (orders.isEmpty()) return;

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

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

    }

}