1"""ll-issues count: Count issues with optional type/priority/status filters."""

2

3 from __future__ import annotations

4

5 import argparse

6 from typing import TYPE_CHECKING

7

8 from little_loops.cli.output import print_json

9

10 if TYPE_CHECKING:

11 from little_loops.config import BRConfig

12

13

14 def cmd_count(config: BRConfig, args: argparse.Namespace) -> int:

15 """Print issue counts with optional status, type, and priority filters.

16

17 Args:

18 config: Project configuration

19 args: Parsed arguments with optional .type, .priority, .status, and .json attributes

20

21 Returns:

22 Exit code (0 = success)

23 """

24 from little_loops.cli.issues.search import _load_issues_with_status

25

26 status = getattr(args, "status", "open") or "open"

27 include_open = status in ("open", "in_progress", "blocked", "all")

28 include_done = status in ("done", "cancelled", "all")

29 include_deferred = status in ("deferred", "all")

30

31 raw = _load_issues_with_status(config, include_open, include_done, include_deferred)

32 issues = [issue for issue, _stat in raw]

33

34 from little_loops.cli_args import parse_priorities

35

36 if getattr(args, "type", None):

37 issues = [i for i in issues if i.issue_id.split("-", 1)[0] == args.type]

38

39 priority_filter = parse_priorities(getattr(args, "priority", None))

40 if priority_filter is not None:

41 issues = [i for i in issues if i.priority in priority_filter]

42

43 if getattr(args, "json", False):

44 by_type: dict[str, int] = {"BUG": 0, "FEAT": 0, "ENH": 0, "EPIC": 0}

45 by_priority: dict[str, int] = {

46 "P0": 0,

47 "P1": 0,

48 "P2": 0,

49 "P3": 0,

50 "P4": 0,

51 "P5": 0,

52 }

53 for issue in issues:

54 prefix = issue.issue_id.split("-", 1)[0]

55 if prefix in by_type:

56 by_type[prefix] += 1

57 if issue.priority in by_priority:

58 by_priority[issue.priority] += 1

59

60 print_json(

61 {"total": len(issues), "status": status, "by_type": by_type, "by_priority": by_priority}

62 )

63 return 0

64

65 print(len(issues))

66 return 0