"""CLI entry point for anki-yaml-sync."""

import click
import logging
import sys

from .commands.import_cmd import import_notes
from .commands.export import export_notes
from . import __version__


# Setup logging
def setup_logging(verbose: bool = False):
    """Configure logging."""
    level = logging.DEBUG if verbose else logging.INFO
    logging.basicConfig(
        level=level,
        format='%(levelname)s: %(message)s'
    )


@click.group(invoke_without_command=True)
@click.option('--verbose', '-v', is_flag=True, help='Enable verbose output')
@click.option('--version', is_flag=True, help='Show version')
@click.pass_context
def main(ctx, verbose, version):
    """Anki YAML Sync - Import/export Anki notes as YAML files."""
    
    if version:
        print(f"anki-yaml-sync version {__version__}")
        sys.exit(0)
    
    setup_logging(verbose)
    
    if ctx.invoked_subcommand is None:
        print(ctx.get_help())


@main.command()
@click.option(
    '--output-dir', '-o',
    default='notes',
    help='Directory to save YAML files (default: notes)'
)
def import_cmd(output_dir):
    """Import notes from Anki and save as YAML files."""
    try:
        import_notes(output_dir)
    except Exception as e:
        sys.exit(1)


@main.command()
@click.option(
    '--input-dir', '-i',
    default='notes',
    help='Directory containing YAML files (default: notes)'
)
@click.option(
    '--deck',
    default='Default',
    help='Deck to add new notes to (default: Default)'
)
def export_cmd(input_dir, deck):
    """Sync YAML files back to Anki."""
    try:
        export_notes(input_dir, deck)
    except Exception as e:
        sys.exit(1)


if __name__ == '__main__':
    main()
