"""Package 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 package():
    """Package management."""
    pass


@package.command()
@click.argument('path')
@click.option('--server', help='Server name from config')
def install(path, server):
    """Install package."""
    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'package install {path}')
    click.echo(result)


@package.command()
@click.argument('name')
@click.option('--server', help='Server name from config')
def uninstall(name, server):
    """Uninstall package."""
    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'package uninstall {name}')
    click.echo(result)


@package.command()
@click.option('--server', help='Server name from config')
def list(server):
    """List installed packages."""
    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('package list')
    click.echo(result)


@package.command()
@click.option('--server', help='Server name from config')
def path(server):
    """Get package path."""
    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('package path')
    click.echo(result)
