from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from blends.stack.bridge_paths import compute_bridge_paths_from_view
from blends.stack.partial_path.minimal_paths import (
    PartialPathLimits,
    compute_minimal_partial_paths_in_file,
)
from blends.stack.view import StackGraphView

if TYPE_CHECKING:
    from blends.models import StackGraph
    from blends.stack.partial_path.minimal_paths import PerFilePartialPaths
    from blends.stack.partial_path_database import FileHandle, PartialPathDatabase


@dataclass(frozen=True, slots=True)
class PartialPathIndexRequest:
    file_path: str
    file_handle: FileHandle
    limits: PartialPathLimits | None = None
    record_edges: bool = True


def index_partial_paths_for_file(
    stack_graph: StackGraph,
    request: PartialPathIndexRequest,
    database: PartialPathDatabase,
    view: StackGraphView | None = None,
) -> PerFilePartialPaths:
    if view is None:
        view = StackGraphView.from_stack_graph(
            stack_graph, path=request.file_path, symbols=database.symbols
        )
    result = compute_minimal_partial_paths_in_file(
        view,
        request.file_path,
        limits=request.limits,
        record_edges=request.record_edges,
    )
    database.put_file(
        file_handle=request.file_handle,
        file_path=request.file_path,
        view=view,
        partial_paths=result,
        bridge_paths=compute_bridge_paths_from_view(view),
    )
    return result
