import json
from dataclasses import asdict


def format_info_human(info) -> str:
    lines = [
        f"File:      {info.path}",
        f"Driver:    {info.driver}",
        f"CRS:       {info.crs}",
        f"Geometry:  {info.geometry_type}",
        f"Features:  {info.feature_count}",
        f"Extent:    {info.bounds[0]:.6f}, {info.bounds[1]:.6f}, {info.bounds[2]:.6f}, {info.bounds[3]:.6f}",
    ]

    if info.fields:
        lines.append("")
        lines.append("Fields:")
        name_width = max(len(f.name) for f in info.fields)
        for f in info.fields:
            lines.append(f"  {f.name:<{name_width}}  {f.type}")

    return "\n".join(lines)


def format_info_json(info) -> str:
    d = asdict(info)
    d["bounds"] = list(d["bounds"])
    return json.dumps(d, indent=2)


def format_table(headers: list[str], rows: list[list[str]]) -> str:
    widths = [len(h) for h in headers]
    for row in rows:
        for i, cell in enumerate(row):
            widths[i] = max(widths[i], len(cell))

    header_line = "  ".join(h.ljust(w) for h, w in zip(headers, widths))
    sep_line = "  ".join("-" * w for w in widths)
    body_lines = [
        "  ".join(cell.ljust(w) for cell, w in zip(row, widths))
        for row in rows
    ]
    return "\n".join([header_line, sep_line] + body_lines)
