from os import getcwd
import git
import argparse
from rich import print, box
from rich.table import Table
from bossman import Bossman
from bossman.abc import ResourceStatusABC

def init(subparsers: argparse._SubParsersAction):
  parser = subparsers.add_parser("status", help="show resource status")
  parser.add_argument("-e", "--exact-match", action="store_true", default=False, help="match resource exactly")
  parser.add_argument("glob", nargs="*", default="*", help="select resources by glob pattern")
  parser.set_defaults(func=exec)

def exec(bossman: Bossman, glob, exact_match:bool, *args, **kwargs):
  resources = bossman.get_resources(*glob, exact_match=exact_match)
  if len(resources) == 0:
    print('no resources selected')
    return
  if len(resources):
    print("On branch", bossman.get_current_branch())

    statuses = bossman.get_resource_statuses(resources)

    col_width = min(max(len(resource.path) for resource in resources) + 2, 60)
    table = Table(expand=False, box=box.HORIZONTALS, show_lines=True)
    table.add_column("Resource", width=col_width)
    table.add_column("Status")
    for resource, status in zip(resources, statuses):
      table.add_row(resource, status)
    print(table)
  else:
    print("No resources to show: check the glob pattern if provided, or the configuration.")
