package dev.ui;

import dev.model.MenuItem;
import dev.model.User;
import dev.service.AuthService;
import dev.service.MenuService;
import dev.service.OrderService;

import java.util.Scanner;

public class ConsoleMenu {

    private final Scanner scanner =
            new Scanner(System.in);
    private final AuthService authService = new AuthService();
    private final MenuService menuService = new MenuService();
    private final OrderService orderService = new OrderService();

    public void start() {

        while (true) {

            System.out.println();
            System.out.println("===== JAVA PIZZERIA =====");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");

            int choice = scanner.nextInt();

            switch (choice) {

                case 1:
                    handleLogin();
                    break;

                case 2:
                    handleRegister();
                    break;

                case 3:
                    return;

                default:
                    System.out.println("Invalid choice");

            }

        }

    }

    private void handleRegister() {
        System.out.print("Choose Username: ");
        String user = scanner.nextLine();
        System.out.print("Choose Password: ");
        String pass = scanner.nextLine();
        System.out.print("Email (Optional, press Enter to skip): ");
        String email = scanner.nextLine();

        if (authService.register(user, pass, email)) {
            System.out.println("Registration successful! You can now log in.");
        }
    }

    private void handleLogin() {
        System.out.print("Username: ");
        String user = scanner.nextLine();
        System.out.print("Password: ");
        String pass = scanner.nextLine();

        User loggedInUser = authService.login(user, pass);
        if (loggedInUser != null) {
            System.out.println("Welcome back, " + loggedInUser.getUsername() + "!");
            showCustomerDashboard(loggedInUser);
        } else {
            System.out.println("Invalid username or password.");
        }
    }

    private void showCustomerDashboard(User user) {
        while (true) {
            System.out.println("\n===== CUSTOMER MENU =====");
            System.out.println("1. Browse Menu");
            System.out.println("2. Place New Order");
            System.out.println("3. View Order History");
            System.out.println("4. Logout");
            System.out.print("Choose an option: ");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    menuService.showMenu();
                    break;
                case 2:
                    menuService.showMenu();
                    orderService.placeOrder(user.getId());
                    break;
                case 3:
                    orderService.showOrderHistory(user.getId());
                    break;
                case 4:
                    System.out.println("Logged out successfully.");
                    return;
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }

}