"""Query planner — translates parsed MQL ASTs into execution plans."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict, List, Optional

from .parser import MemorizeCommand, RecallQuery


@dataclass
class RecallPlan:
    """Execution plan for a RECALL query."""

    query_text: Optional[str] = None
    similarity_threshold: float = 0.0
    min_importance: float = 0.0
    entity_filter: List[str] = field(default_factory=list)
    time_window_days: Optional[int] = None
    scope: Optional[str] = None
    limit: int = 10
    projected_fields: List[str] = field(default_factory=lambda: ["*"])


@dataclass
class MemorizePlan:
    """Execution plan for a MEMORIZE command."""

    content: str = ""
    entities: List[str] = field(default_factory=list)
    scope: str = "global"
    auto_score: bool = True


def plan(ast: RecallQuery | MemorizeCommand) -> RecallPlan | MemorizePlan:
    """Convert a parsed AST into an execution plan."""
    if isinstance(ast, RecallQuery):
        return RecallPlan(
            query_text=ast.similar_to,
            similarity_threshold=ast.similarity_threshold,
            min_importance=ast.min_importance,
            entity_filter=ast.entity_match,
            time_window_days=ast.time_window_days,
            scope=ast.scope,
            limit=ast.limit,
            projected_fields=ast.fields,
        )
    elif isinstance(ast, MemorizeCommand):
        return MemorizePlan(
            content=ast.content,
            entities=ast.entities,
            scope=ast.scope,
            auto_score=ast.auto_score,
        )
    else:
        raise TypeError(f"Unknown AST type: {type(ast)}")
