from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(frozen=True)
class FocusConfig:
    enabled: bool = False
    policy: str = "energy_first"
    serial: bool = True
    candidate_limit: int = 8
    allow_anchor_map: bool = True
    inhibition_of_return_ticks: int = 3
    anchor_map_capacity: int = 8
    anchor_decay_ticks: int = 64


@dataclass(frozen=True)
class FocusCandidate:
    point: tuple[int, int] | None = None
    scalar_value: float | None = None
    energy: float = 0.0
    novelty_score: float = 0.0
    familiarity_score: float = 0.0
    task_bias: float = 0.0
    last_visited_penalty: float = 0.0


@dataclass(frozen=True)
class AnchorState:
    kind: str
    point: tuple[int, int] | None = None
    scalar_value: float | None = None
    created_tick: int = 0
    updated_tick: int = 0
    visit_count: int = 0
    energy: float = 0.0
    policy_name: str | None = None


@dataclass(frozen=True)
class FocusSelection:
    candidate: FocusCandidate
    policy_name: str
    selected_rank: int
    anchor_before: AnchorState | None
    anchor_after: AnchorState | None
    distance_from_anchor: float
    inhibited: bool = False


@dataclass(frozen=True)
class FocusTickSummary:
    policy_name: str | None = None
    candidate_count: int = 0
    selection_count: int = 0
    active_focus_point: tuple[int, int] | None = None
    active_focus_value: float | None = None
    active_anchor_point: tuple[int, int] | None = None
    active_anchor_value: float | None = None
    anchor_count: int = 0
    selected_points: tuple[tuple[int, int], ...] = field(default_factory=tuple)
    selected_values: tuple[float, ...] = field(default_factory=tuple)


def point_payload(point_value: tuple[int, int] | None) -> list[int] | None:
    if point_value is None:
        return None
    return [int(point_value[0]), int(point_value[1])]

