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();
            System.out.println("===== RESTAURANT SYSTEM =====");
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.println("0. Exit");
            System.out.print("Choose: ");

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

            if (choice == 1) {
                register();
            } else if (choice == 2) {
                login();
            } else if (choice == 0) {
                System.out.println("Goodbye!");
                return;
            } else {
                System.out.println("Invalid choice.");
            }
        }
    }

    private void register() {

        System.out.print("Username: ");
        String username = scanner.nextLine();

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

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

        boolean success = authService.register(username, password, email);

        if (success) {
            System.out.println("Registration successful.");
        } else {
            System.out.println("Registration failed.");
        }
    }

    private void 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("Invalid username or password.");
            return;
        }

        System.out.println("Login successful. Welcome " + user.getUsername());

        userMenu(user);
    }

    private void userMenu(User user) {

        while (true) {

            System.out.println();
            System.out.println("===== USER MENU =====");
            System.out.println("1. View Menu");
            System.out.println("2. Place Order");
            System.out.println("3. Order History");
            System.out.println("0. Logout");
            System.out.print("Choose: ");

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

            if (choice == 1) {
                menuService.showMenu();
            } else if (choice == 2) {
                menuService.showMenu();
                orderService.placeOrder(user.getId());
            } else if (choice == 3) {
                orderService.showOrderHistory(user.getId());
            } else if (choice == 0) {
                System.out.println("Logged out.");
                return;
            } else {
                System.out.println("Invalid choice.");
            }
        }
    }
}