import click

from geo.core.reader import read_dataset as open_dataset
from geo.core.writer import write_dataset as open_output
from geo.formats import driver_for_path


@click.command()
@click.argument("input_path", type=click.Path(exists=True))
@click.argument("output_path", type=click.Path())
@click.option("--crs", default=None, help="Target CRS (e.g. EPSG:4326)")
@click.pass_context
def convert(ctx, input_path, output_path, crs):
    """Convert between geospatial formats.

    Optionally reproject to a target CRS during conversion.
    """
    gdf = open_dataset(input_path)
    
    if crs:
        gdf = gdf.to_crs(crs)

    target_driver = driver_for_path(output_path)
    open_output(gdf, output_path, driver=target_driver)

    if not ctx.obj.get("quiet"):
        click.echo(f"Converted {len(gdf)} features → {output_path} [{target_driver}]",
                   err=True)
