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 MenuItemDao menuDao =
            new MenuItemDao();


    private final OrderDao orderDao =
            new OrderDao();


    private final OrderDetailDao detailDao =
            new OrderDetailDao();



    public int placeOrder(int userId){


        Scanner scanner =
                new Scanner(System.in);



        List<OrderDetail> cart =
                new ArrayList<>();


        double total = 0;



        while(true){


            System.out.println(
                    "Enter menu item id (0 finish): "
            );


            int itemId =
                    scanner.nextInt();



            if(itemId == 0){

                break;

            }



            MenuItem item =
                    menuDao.findById(itemId);



            if(item == null){

                System.out.println(
                        "Item not found!"
                );

                continue;

            }



            System.out.println(
                    "Enter quantity:"
            );


            int quantity =
                    scanner.nextInt();



            OrderDetail detail =
                    new OrderDetail(
                            0,
                            item.getId(),
                            quantity,
                            item.getPrice()
                    );



            cart.add(detail);



            total +=
                    item.getPrice()
                            *
                            quantity;



            System.out.println(
                    "Added to cart."
            );


        }



        if(cart.isEmpty()){

            System.out.println(
                    "Cart is empty"
            );

            return -1;

        }



        // Save Order first

        Order order =
                new Order(
                        userId,
                        total
                );



        int orderId =
                orderDao.save(order);





        for(OrderDetail detail:cart){


            detail.setOrderId(orderId);


            detailDao.save(detail);


        }



        System.out.println(
                "Order created successfully!"
        );


        printReceipt(orderId);



        return orderId;


    }







    public void printReceipt(int orderId){



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



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



        double total = 0;



        for(OrderDetail d:details){



            MenuItem item =
                    menuDao.findById(
                            d.getMenuItemId()
                    );



            double subtotal =
                    d.getQuantity()
                            *
                            d.getPrice();



            total += subtotal;



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

        }



        System.out.println(
                "----------------------------"
        );


        System.out.println(
                "Grand Total: $"
                        +total
        );


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


    }







    public void showOrderHistory(int userId){



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



        for(Order order:
                orderDao.findByUserId(userId)){



            System.out.println(
                    "Order ID: "
                            +order.getId()
                            +
                            " Total: $"+
                            order.getTotalPrice()
            );


        }


    }


}