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;
    private final MenuService menuService;
    private final OrderService orderService;
    private User currentUser;

    public ConsoleMenu() {
        this.authService = new AuthService();
        this.menuService = new MenuService();
        this.orderService = new OrderService();
    }

    public void start() {
        while (true) {
            if (currentUser == null) {
                showAuthMenu();
            } else {
                showMainMenu();
            }
        }
    }

    private void showAuthMenu() {
        System.out.println();
        System.out.println("      ===== JAVA PIZZERIA =====");
        System.out.println("1. Login");
        System.out.println("2. Register New Account");
        System.out.println("3. Exit");
        System.out.print("Choose an option: ");

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

        switch (choice) {
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            case 3:
                System.out.println("Goodbye!");
                System.exit(0);
                break;
            default:
                System.out.println("Invalid choice!");
        }
    }

    private void showMainMenu() {
        System.out.println();
        System.out.println("           MAIN MENU");
        System.out.println("1. View Menu");
        System.out.println("2. Place a 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:
                orderService.placeOrder(currentUser.getId());
                break;
            case 3:
                orderService.showOrderHistory(currentUser.getId());
                break;
            case 4:
                logout();
                break;
            default:
                System.out.println("Invalid choice!");
        }
    }

    private void login() {
        System.out.println("\n[Login]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();

        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        User user = authService.login(username, password);
        if (user != null) {
            currentUser = user;
        }
    }

    private void register() {
        System.out.println("\n[Register]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();

        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        System.out.print("Enter email (optional): ");
        String email = scanner.nextLine();

        if (email == null || email.trim().isEmpty()) {
            email = null;
        }

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Please login with your new account.");
        }
    }

    private void logout() {
        System.out.println("Goodbye, " + currentUser.getUsername() + "!");
        currentUser = null;
    }
}