import click

from src.backup_manager import BackupManager
from src.compressor import CompressionLevel


@click.group()
def cli():
    """Backup Manager CLI for creating and restoring backups."""
    pass


@cli.command()
@click.argument('source', type=click.Path(exists=False, file_okay=False, dir_okay=True))
@click.argument('destination', type=click.Path())
@click.option('--level', type=click.Choice(['fastest', 'normal', 'best'], case_sensitive=False), default='normal',
              help='Compression level.')
@click.option('--parallel', is_flag=True, help='Enable parallel compression.')
@click.option('--encryption-key', type=click.Path(exists=True, dir_okay=False), help='Path to encryption key.')
def backup(source, destination, level, parallel, encryption_key):
    """Create a backup from SOURCE directory and save it to DESTINATION."""
    level_map = {'fastest': CompressionLevel.fastest, 'normal': CompressionLevel.normal, 'best': CompressionLevel.best}
    manager = BackupManager(
        source,
        destination,
        level=level_map[level],
        parallel=parallel,
        encryption_key_path=encryption_key,
    )
    try:
        click.echo(f"Creating backup of {source}...")
        manager.create_backup()
        click.echo(f"Backup created successfully at {destination}")
    except FileNotFoundError as e:
        click.echo(f"Backup creation failed: {e}")
        exit(1)


@cli.command()
@click.argument('backup_file', type=click.Path(exists=True, dir_okay=False))
@click.argument('restore_dir', type=click.Path(exists=False, file_okay=False, dir_okay=True))
def restore(backup_file, restore_dir):
    """Restore a backup from BACKUP_FILE to RESTORE_DIR."""
    manager = BackupManager("", backup_file)  # Only need destination in this case
    click.echo("Restoring backup...")
    manager.restore_backup(restore_dir)
    click.echo(f"Backup restored successfully to {restore_dir}")
