"""WordPress block management commands"""
import click

from praisonaiwp.core.transport import get_transport
from praisonaiwp.core.config import Config
from praisonaiwp.core.wp_client import WPClient


@click.group()
def block():
    """Manage WordPress blocks."""
    pass


@block.command()
@click.option('--server', help='Server name from config')
def list(server):
    """List available blocks."""
    config = Config()
    transport = get_transport(config, server)
    transport.connect()
    client = WPClient(ssh, config.get_server(server)['wp_path'] if server else None)
    
    result = client.cli('block list')
    click.echo(result)


@block.command()
@click.argument('block_name')
@click.option('--server', help='Server name from config')
def get(block_name, server):
    """Get block information."""
    config = Config()
    transport = get_transport(config, server)
    transport.connect()
    client = WPClient(ssh, config.get_server(server)['wp_path'] if server else None)
    
    result = client.cli(f'block get {block_name}')
    click.echo(result)


@block.command()
@click.argument('block_name')
@click.option('--server', help='Server name from config')
def register(block_name, server):
    """Register a block."""
    config = Config()
    transport = get_transport(config, server)
    transport.connect()
    client = WPClient(ssh, config.get_server(server)['wp_path'] if server else None)
    
    result = client.cli(f'block register {block_name}')
    click.echo(result)


@block.command()
@click.argument('block_name')
@click.option('--server', help='Server name from config')
def unregister(block_name, server):
    """Unregister a block."""
    config = Config()
    transport = get_transport(config, server)
    transport.connect()
    client = WPClient(ssh, config.get_server(server)['wp_path'] if server else None)
    
    result = client.cli(f'block unregister {block_name}')
    click.echo(result)
