"""clusters 命令 - 管理和查看 S3 集群配置"""
from rich.table import Table
from rich.console import Console

from .config import load_clusters, CREDENTIALS_FILE, ClusterConfig
from . import client as client_module

console = Console()


class Clusters:
    """管理 S3 集群配置"""

    def list(self) -> None:
        """列出所有可用的 S3 集群配置

        示例:
            s3c clusters list
        """
        clusters = load_clusters()

        if not clusters:
            console.print(f"[yellow]暂无配置，请创建配置文件: {CREDENTIALS_FILE}[/yellow]")
            console.print("\n示例格式:")
            console.print_json(
                '[{"name": "default", "description": "默认配置",'
                ' "endpoint_url": "https://s3.amazonaws.com",'
                ' "access_key": "YOUR_KEY", "secret_key": "YOUR_SECRET",'
                ' "region": "us-east-1"}]'
            )
            return

        table = Table(title="S3 集群配置列表", show_lines=True)
        table.add_column("默认", style="green", width=4, justify="center")
        table.add_column("名称", style="cyan", min_width=10)
        table.add_column("描述", min_width=15)
        table.add_column("Endpoint URL", min_width=25)
        table.add_column("区域", min_width=10)

        for i, cluster in enumerate(clusters):
            is_default = "★" if i == 0 else ""
            table.add_row(
                is_default,
                cluster.name,
                cluster.description,
                cluster.endpoint_url,
                cluster.region,
            )

        console.print(table)
        console.print(f"\n[dim]配置文件: {CREDENTIALS_FILE}[/dim]")
