/**
 * EntityExtractor - Entity extractor 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.*;

/**
 * Extracts entities from user messages.
 */
public class EntityExtractor {

    /**
     * Represents an extracted entity.
     */
    public static class Entity {
        public final String type;
        public final String value;

        public Entity(String type, String value) {
            this.type = type;
            this.value = value;
        }

        @Override
        public String toString() {
            return String.format("%s:%s", type, value);
        }
    }

    // Map of regex patterns for each entity type
    private static final Map<String, Pattern> ENTITY_PATTERNS = new HashMap<>();

    static {
        // Food and cuisine types
        ENTITY_PATTERNS.put("food", Pattern.compile(
            "\\b(pizza|sushi|steak|steakhouse|italian|japanese|diner|bistro|tavern|pub|bar)\\b",
            Pattern.CASE_INSENSITIVE
        ));

        // Price range
        ENTITY_PATTERNS.put("price", Pattern.compile(
            "\\b(cheap|medium|expensive|budget|affordable|moderate|luxury|gourmet|premium)\\b",
            Pattern.CASE_INSENSITIVE
        ));

        // Area / location
        ENTITY_PATTERNS.put("area", Pattern.compile(
            "\\b(north|south|east|west|downtown|center|central)\\b",
            Pattern.CASE_INSENSITIVE
        ));

        // Date
        ENTITY_PATTERNS.put("date", Pattern.compile(
            "\\b(today|tomorrow|yesterday|monday|tuesday|wednesday|thursday|friday|saturday|sunday)\\b",
            Pattern.CASE_INSENSITIVE
        ));

        // Time
        ENTITY_PATTERNS.put("time", Pattern.compile(
            "(\\d{1,2}:\\d{2}|\\b\\d{1,2}\\s?(?:am|pm)\\b|\\bmorning\\b|\\bafternoon\\b|\\bevening\\b|\\bnight\\b|\\bnoon\\b|\\bmidnight\\b)",
            Pattern.CASE_INSENSITIVE
        ));

        // Number of people
        ENTITY_PATTERNS.put("people", Pattern.compile(
            "\\b(\\d+)\\b", Pattern.CASE_INSENSITIVE
        ));

        // Restaurant names
        ENTITY_PATTERNS.put("restaurant", Pattern.compile(
            "\\b(Pizza Place|Sushi Corner|Prime Steakhouse|Corner Diner|Italian Bistro|Downtown Tavern)\\b",
            Pattern.CASE_INSENSITIVE
        ));
    }

    /**
     * Extracts entities from a message.
     * @param message The user's message
     * @return List of extracted entities
     */
    public static List<Entity> extract(String message) {
        List<Entity> entities = new ArrayList<>();

        for (Map.Entry<String, Pattern> entry : ENTITY_PATTERNS.entrySet()) {
            Matcher matcher = entry.getValue().matcher(message);
            while (matcher.find()) {
                entities.add(new Entity(entry.getKey(), matcher.group()));
            }
        }

        return entities;
    }
}
