from __future__ import annotations

from pydantic.dataclasses import dataclass

# TODO these data models should prob be shared between aiservice and cli


@dataclass(frozen=True)
class FunctionParent:
    name: str
    type: str


@dataclass(frozen=True, config={"arbitrary_types_allowed": True})
class FunctionToOptimize:
    function_name: str
    file_path: str
    parents: list[FunctionParent]  # List[ClassDef | FunctionDef | AsyncFunctionDef]
    starting_line: int | None = None
    ending_line: int | None = None
    is_async: bool = False

    # For "BubbleSort.sorter", returns "BubbleSort"
    # For "sorter", returns "sorter"
    # TODO does not support nested classes or functions
    @property
    def top_level_parent_name(self) -> str:
        if self.parents:
            return self.parents[0].name
        return self.function_name

    @property
    def qualified_name(self) -> str:
        """Return the fully qualified name of the function, including parent classes/functions."""
        if not self.parents:
            return self.function_name
        parent_names = [parent.name for parent in self.parents]
        return ".".join(parent_names + [self.function_name])

    def __str__(self) -> str:
        return f"{self.file_path}:{'.'.join([p.name for p in self.parents]) + '.' if self.parents else ''}{self.function_name}"
