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();

    private User currentUser = null; // برای مانیتور کردن وضعیت سشن کاربر جاری

    public void start() {
        while (true) {
            if (currentUser == null) {
                // منوی اولیه قبل از لاگین
                System.out.println();
                System.out.println("===== 🍕 WELCOME TO JAVA PIZZERIA 🍕 =====");
                System.out.println("1. Login");
                System.out.println("2. Register New Account");
                System.out.println("3. Exit");
                System.out.println("=======================================");
                System.out.print("Choose an option: ");

                int choice = scanner.nextInt();
                scanner.nextLine(); // مصرف کردن کلید اینتر مابقی هدر کلاینت

                switch (choice) {
                    case 1:
                        handleLogin();
                        break;
                    case 2:
                        handleRegister();
                        break;
                    case 3:
                        System.out.println("Goodbye!");
                        return;
                    default:
                        System.out.println("Invalid choice! Please select a valid number.");
                }
            } else {
                // منوی اصلی سیستم بعد از لاگین موفق کاربر طبق ساختار تمپلت داک
                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.println("=======================================");
                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:
                        System.out.println("Logged out successfully.");
                        currentUser = null; // ریست کردن سشن کابر جاری
                        break;
                    default:
                        System.out.println("Invalid choice! Please select a valid number.");
                }
            }
        }
    }

    private void handleLogin() {
        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;
            System.out.println("Welcome back, " + currentUser.getUsername() + "!");
        }
    }

    private void handleRegister() {
        System.out.println("\n[Register New Account]");
        System.out.print("Enter username: ");
        String username = scanner.nextLine();
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        System.out.print("Enter email (optional, press Enter to skip): ");
        String email = scanner.nextLine();
        if (email.trim().isEmpty()) {
            email = null;
        }

        boolean success = authService.register(username, password, email);
        if (success) {
            System.out.println("Account created successfully! You can now log in.");
        } else {
            System.out.println("Registration failed.");
        }
    }
}