package dev.ui;

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("\n===========================");
            System.out.println("   🍕 JAVA PIZZERIA 🍕");
            System.out.println("===========================");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");
            System.out.print("Choose: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1 -> {
                    User user = handleLogin();
                    if (user != null) showMainMenu(user);
                }
                case 2 -> handleRegister();
                case 3 -> { System.out.println("Goodbye!"); return; }
                default -> System.out.println("Invalid choice.");
            }
        }
    }

    private void handleRegister() {
        System.out.println("\n[Register]");
        System.out.print("Username: "); String username = scanner.nextLine();
        System.out.print("Password: "); String password = scanner.nextLine();
        System.out.print("Email (optional, press Enter to skip): "); String email = scanner.nextLine();

        if (authService.register(username, password, email)) {
            System.out.println("Registration successful! Please log in.");
        }
    }

    private User handleLogin() {
        System.out.println("\n[Login]");
        System.out.print("Username: "); String username = scanner.nextLine();
        System.out.print("Password: "); String password = scanner.nextLine();

        User user = authService.login(username, password);
        if (user != null) System.out.println("Welcome, " + user.getUsername() + "!");
        return user;
    }

    private void showMainMenu(User user) {
        while (true) {
            System.out.println("\n===========================");
            System.out.println("       🍽️ MAIN MENU 🍽️");
            System.out.println("===========================");
            System.out.println("1. View Menu");
            System.out.println("2. Place Order");
            System.out.println("3. Order History");
            System.out.println("4. Logout");
            System.out.print("Choose: ");

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

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