/**
 * IntentClassifier - Intent classifier for the final project
 *
 * Part of Part III of the book "Introduction to Programming in Java"
 */

package chatbot;

import java.util.*;
import java.util.regex.*;

/**
 * Classifies user messages into intents.
 */
public class IntentClassifier {

    /**
     * Enum representing the supported intents.
     */
    public enum Intent {
        GREETING,          // Greetings
        GOODBYE,           // Farewells
        THANKS,            // Gratitude
        HELP,              // Help request
        SEARCH_RESTAURANT, // Restaurant search
        BOOK_TABLE,        // Table reservation
        WEATHER,           // Weather inquiry
        JOKE,              // Joke request
        UNKNOWN            // Unknown intent
    }

    // Map of regex patterns for each intent
    private static final Map<Intent, List<Pattern>> PATTERNS = new EnumMap<>(Intent.class);

    static {
        // Greetings
        PATTERNS.put(Intent.GREETING, Arrays.asList(
            Pattern.compile("\\b(hi|hello|good morning|good afternoon|good evening|hey|greetings)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Farewells
        PATTERNS.put(Intent.GOODBYE, Arrays.asList(
            Pattern.compile("\\b(bye|goodbye|see you|farewell|later|take care)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Gratitude
        PATTERNS.put(Intent.THANKS, Arrays.asList(
            Pattern.compile("\\b(thanks|thank you|thx|appreciate|grateful)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Help request
        PATTERNS.put(Intent.HELP, Arrays.asList(
            Pattern.compile("\\b(help|what can you do|how to use|commands|options)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Restaurant search
        PATTERNS.put(Intent.SEARCH_RESTAURANT, Arrays.asList(
            Pattern.compile("\\b(restaurant|eat|dinner|lunch|pizza|sushi|steak|steakhouse|diner|bistro|tavern|italian)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Table reservation
        PATTERNS.put(Intent.BOOK_TABLE, Arrays.asList(
            Pattern.compile("\\b(book|reserve|reservation|table|seat|make a reservation)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Weather inquiry
        PATTERNS.put(Intent.WEATHER, Arrays.asList(
            Pattern.compile("\\b(weather|forecast|rain|sunny|cold|hot|snow|temperature)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Joke request
        PATTERNS.put(Intent.JOKE, Arrays.asList(
            Pattern.compile("\\b(joke|funny|laugh|humor|tell me a joke)\\b", Pattern.CASE_INSENSITIVE)
        ));
    }

    /**
     * Classifies a message into an intent.
     * @param message The user's message
     * @return The classified intent
     */
    public static Intent classify(String message) {
        String normalized = message.toLowerCase().trim();

        for (Map.Entry<Intent, List<Pattern>> entry : PATTERNS.entrySet()) {
            for (Pattern pattern : entry.getValue()) {
                if (pattern.matcher(normalized).find()) {
                    return entry.getKey();
                }
            }
        }

        return Intent.UNKNOWN;
    }
}
