from fnmatch import fnmatch
from heuristics.heuristic_base import Heuristic

def get_parts(fact):
    """Extract the components of a PDDL fact by removing parentheses and splitting the string."""
    return fact[1:-1].split()

def match(fact, *args):
    """
    Check if a PDDL fact matches a given pattern.

    - `fact`: The complete fact as a string, e.g., "(at tray1 kitchen)".
    - `args`: The expected pattern (wildcards `*` allowed).
    - Returns `True` if the fact matches the pattern, else `False`.
    """
    parts = get_parts(fact)
    return all(fnmatch(part, arg) for part, arg in zip(parts, args))

class ChildsnackHeuristic(Heuristic):
    """
    A domain-dependent heuristic for the Childsnacks domain.

    # Summary
    This heuristic estimates the number of actions required to serve all children with sandwiches, considering the constraints of gluten allergies and the availability of ingredients.

    # Assumptions:
    - Each child must be served exactly one sandwich.
    - Children with gluten allergies must be served gluten-free sandwiches.
    - Sandwiches must be made from available bread and content portions in the kitchen.
    - Sandwiches must be placed on trays and moved to the appropriate tables before serving.

    # Heuristic Initialization
    - Extract the goal conditions (served children) and static facts (allergic children, waiting children, etc.).
    - Identify the number of children, trays, and available ingredients.

    # Step-By-Step Thinking for Computing Heuristic
    1. Count the number of children who are not yet served.
    2. For each unserved child:
       - If the child is allergic to gluten, ensure a gluten-free sandwich is available.
       - If the child is not allergic, any sandwich can be used.
    3. Count the number of sandwiches that need to be made:
       - For each required sandwich, check if the necessary ingredients are available in the kitchen.
    4. Count the number of trays needed to carry the sandwiches to the tables:
       - Each tray can carry multiple sandwiches, but each sandwich must be placed on a tray.
    5. Estimate the number of actions required:
       - Making sandwiches: 1 action per sandwich.
       - Placing sandwiches on trays: 1 action per sandwich.
       - Moving trays to tables: 1 action per tray.
       - Serving sandwiches: 1 action per child.
    """

    def __init__(self, task):
        """Initialize the heuristic by extracting goal conditions and static facts."""
        self.goals = task.goals  # Goal conditions.
        self.static = task.static  # Static facts.

        # Extract information about children and their allergies.
        self.allergic_children = {
            get_parts(fact)[1] for fact in self.static if match(fact, "allergic_gluten", "*")
        }
        self.not_allergic_children = {
            get_parts(fact)[1] for fact in self.static if match(fact, "not_allergic_gluten", "*")
        }

        # Extract waiting children and their locations.
        self.waiting_children = {
            get_parts(fact)[1]: get_parts(fact)[2] for fact in self.static if match(fact, "waiting", "*", "*")
        }

    def __call__(self, node):
        """Estimate the number of actions required to serve all children."""
        state = node.state  # Current world state.

        # Count the number of children who are not yet served.
        unserved_children = {
            child for child in self.waiting_children if f"(served {child})" not in state
        }

        # Count the number of sandwiches that need to be made.
        sandwiches_needed = 0
        for child in unserved_children:
            if child in self.allergic_children:
                # Gluten-free sandwich required.
                sandwiches_needed += 1
            else:
                # Any sandwich will do.
                sandwiches_needed += 1

        # Count the number of sandwiches that can be made with available ingredients.
        available_bread = sum(1 for fact in state if match(fact, "at_kitchen_bread", "*"))
        available_content = sum(1 for fact in state if match(fact, "at_kitchen_content", "*"))
        sandwiches_possible = min(available_bread, available_content)

        # Estimate the number of actions required.
        total_cost = 0

        # Actions to make sandwiches.
        total_cost += min(sandwiches_needed, sandwiches_possible)

        # Actions to place sandwiches on trays.
        total_cost += min(sandwiches_needed, sandwiches_possible)

        # Actions to move trays to tables.
        trays_needed = (sandwiches_needed + 1) // 2  # Assuming each tray can carry 2 sandwiches.
        total_cost += trays_needed

        # Actions to serve sandwiches.
        total_cost += len(unserved_children)

        return total_cost
