from __future__ import annotations

import argparse
import json

from model_chat_server import health_status


def print_table(services: list[dict]) -> None:
    rows = []
    for item in services:
        service = item.get("service") or {}
        storage = item.get("storage") or {}
        rows.append(
            [
                str(item.get("id") or ""),
                str(item.get("status") or ""),
                str(item.get("runtime") or ""),
                str(storage.get("status") or ""),
                str(service.get("base_url") or ""),
                str(service.get("compose") or "-"),
            ]
        )

    headers = ["model", "service", "runtime", "storage", "endpoint", "compose"]
    widths = [
        max(len(headers[index]), *(len(row[index]) for row in rows)) if rows else len(headers[index])
        for index in range(len(headers))
    ]
    print("  ".join(headers[index].ljust(widths[index]) for index in range(len(headers))))
    print("  ".join("-" * width for width in widths))
    for row in rows:
        print("  ".join(row[index].ljust(widths[index]) for index in range(len(row))))


def main() -> int:
    parser = argparse.ArgumentParser(description="Show local model service readiness.")
    parser.add_argument("--json", action="store_true", help="Print full health payload as JSON.")
    args = parser.parse_args()

    status = health_status()
    if args.json:
        print(json.dumps(status, ensure_ascii=False, indent=2))
    else:
        print(f"Status: {status['status']} | host: {status['host']} | models: {status['registry_models']}")
        print_table(status["model_services"])
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
