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 AuthService authService = new AuthService();
    private MenuService menuService = new MenuService();
    private OrderService orderService = new OrderService();
    private User loggedInUser = null;
    private final Scanner scanner =
            new Scanner(System.in);

    public void start() {

        while (true) {
            if (loggedInUser == null) {

                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();
                scanner.nextLine();

                switch (choice) {

                    case 1:
                        login();
                        break;

                    case 2:
                        register();
                        break;

                    case 3:
                        System.out.println("Goodbye!");
                        return;

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

                }
            }else{
                showMainMenu();
            }

        }

    }
    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) {
            loggedInUser = 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();

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Registration successful! You can now login.");
        } else {
            System.out.println("Registration failed.");
        }
    }

    private void showMainMenu() {
        System.out.println();
        System.out.println("=======================================");
        System.out.println("          🍽️ MAIN MENU 🍽️");
        System.out.println("=======================================");
        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(loggedInUser.getId());
                break;
            case 3:
                orderService.showOrderHistory(loggedInUser.getId());
                break;
            case 4:
                loggedInUser = null;
                System.out.println("Logged out.");
                break;
            default:
                System.out.println("Invalid choice.");
        }
    }

}