from src.models import Issue, PullRequest, Repo, SuggestionDelta, User, ViewContext


def _load(user: User, repos: list[Repo], issues: list[Issue], prs: list[PullRequest], view: str) -> ViewContext:
    return ViewContext(user=user, repos=repos, issues=issues, pull_requests=prs, view=view)


def _suggest_review(prs: list[PullRequest]) -> list[SuggestionDelta]:
    deltas: list[SuggestionDelta] = []
    for pr in prs:
        deltas.append(SuggestionDelta(panel="review", action="review", target=str(pr.number), priority="high"))
    return deltas[:5]


def _suggest_triage(issues: list[Issue]) -> list[SuggestionDelta]:
    deltas: list[SuggestionDelta] = []
    for issue in issues:
        if issue.state != "open":
            continue
        priority = "high" if any(label.lower() in {"p0", "priority-high"} for label in issue.labels) else "medium"
        deltas.append(SuggestionDelta(panel="triage", action="open", target=str(issue.number), priority=priority))
    return deltas[:8]


def _suggest_focus(repos: list[Repo], prs: list[PullRequest], issues: list[Issue]) -> list[SuggestionDelta]:
    if len(prs) > len(issues):
        return [SuggestionDelta(panel="layout", action="highlight", target="review", priority="medium")]
    if len(issues) > len(prs):
        return [SuggestionDelta(panel="layout", action="highlight", target="issues", priority="medium")]
    return [SuggestionDelta(panel="layout", action="highlight", target="dashboard", priority="low")]


def compute(user: User, repos: list[Repo], issues: list[Issue], prs: list[PullRequest], view: str = "dashboard") -> ViewContext:
    ctx = _load(user, repos, issues, prs, view)
    ctx.deltas = _suggest_review(prs) + _suggest_triage(issues) + _suggest_focus(repos, prs, issues)
    return ctx
