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 dev.model.Resipt;

import java.time.LocalDateTime;
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<Resipt> resipts = new ArrayList<>();
        double totalPrice= 0.0;

        while (true){
            System.out.println("Enter the ID of the item to add (or 0 to finish):");
            int chooseId = scanner.nextInt();
            if (chooseId == 0) {
                break;
            }
            MenuItem chosen = menuItemDao.findById(chooseId);
            if (chosen == null) {
                System.out.println("❌ Invalid food! Please try again.");
                continue;
            }
            System.out.println("Enter quantity:");
            int quantity = scanner.nextInt();

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

            resipts.add(new Resipt(chosen , quantity));
            totalPrice += chosen.getPrice() * quantity;
            System.out.println("ADD " + quantity + " " + chosen.getName());

        }

        LocalDateTime now = LocalDateTime.now();
        Order order = new Order(userId , now , totalPrice);
        int orderId = orderDao.save(order);

        System.out.println("orderId: "+ orderId);
        for (int i = 0; i < resipts.size() ; i++){
            OrderDetail orderDetail = new OrderDetail(orderId ,
                    resipts.get(i).getMenuItem().getId()

                    ,resipts.get(i).getQuantity()
                    ,resipts.get(i).getMenuItem().getPrice());

            orderDetailDao.save(orderDetail);

        }




        printReceipt(orderId);


    }

    public void printReceipt(int orderId) {

        System.out.println("[Order Summary / Receipt]");
        System.out.println("--------------------------------------------------");
        System.out.println("         Item        |  Quantity  |    Unit    | Total");
        System.out.println("--------------------------------------------------");

        double totalprice = 0.0;
        List<OrderDetail> resipt = orderDetailDao.findByOrderId(orderId);
        for(int i = 0 ; i< resipt.size() ; i++){
            MenuItem menuItem = menuItemDao.findById(resipt.get(i).getMenuItemId());
            System.out.printf("%-20s | %-10d | %-10.2f | %-10.2f" ,menuItem.getName()
            ,resipt.get(i).getQuantity() , menuItem.getPrice() , resipt.get(i).getQuantity()* menuItem.getPrice());

            System.out.println();
            totalprice += resipt.get(i).getQuantity()* menuItem.getPrice();
            System.out.println("--------------------------------------------------");
        }
        System.out.println("Final price: " + totalprice );

    }

    public void showOrderHistory(int userId) {

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

        System.out.printf("%-10s | %-30s | %-10s%n", "Order Id", "Date & Time", "Total Price");
        System.out.println("------------------------------------------------------------");
        for (Order o : orders){
            System.out.printf("%-10d | %-30s | $%-9.2f%n",
                    o.getId(),
                    o.getCreatedAt().toString(),
                    o.getTotalPrice());
        }



    }

}