from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Literal, TypedDict


Language = Literal[
    "python",
    "javascript",
    "typescript",
    "json",
    "yaml",
    "markdown",
    "text",
    "unknown",
]


class Span(TypedDict):
    start_line: int
    end_line: int
    start_col: int
    end_col: int


class ImportRef(TypedDict, total=False):
    module: str
    name: str
    alias: str
    level: int
    span: Span


class SymbolDef(TypedDict, total=False):
    symbol_id: str
    kind: Literal["class", "function", "method"]
    name: str
    qualname: str
    path: str
    span: Span
    signature: str
    decorators: list[str]
    doc_firstline: str
    returns: str
    params: list[str]


class CallRef(TypedDict, total=False):
    caller_symbol_id: str
    callee: str
    span: Span


class FileFacts(TypedDict, total=False):
    path: str
    language: Language
    size_bytes: int
    sha256: str
    imports: list[ImportRef]
    symbols: list[SymbolDef]
    calls: list[CallRef]
    parse_errors: list[str]


class FileAnalysis(TypedDict, total=False):
    summary: str
    responsibilities: list[str]
    external_dependencies: list[str]
    key_interfaces: list[str]
    risks: list[str]
    error: str
    raw: Any


class ProjectMap(TypedDict, total=False):
    meta: dict[str, Any]
    files: dict[str, dict[str, Any]]
    symbols: dict[str, SymbolDef]
    edges: dict[str, Any]


@dataclass(frozen=True)
class FileRecord:
    path: str
    language: Language
    size_bytes: int
    # sha256 is computed in runner (so we can show progress)
    sha256: str = ""

