use std::fs::File;
use std::io::BufReader;

use crate::error::PyGeoArrowResult;
use crate::table::GeoTable;
use geoarrow::io::csv::read_csv as _read_csv;
use pyo3::exceptions::PyFileNotFoundError;
use pyo3::prelude::*;

/// Read a CSV file from a path on disk into a GeoTable.
///
/// Args:
///     path: the path to the file
///     geometry_column_name: the name of the geometry column within the CSV.
///
/// Returns:
///     Table from CSV file.
#[pyfunction]
pub fn read_csv(path: String, geometry_column_name: &str) -> PyGeoArrowResult<GeoTable> {
    let f = File::open(path).map_err(|err| PyFileNotFoundError::new_err(err.to_string()))?;
    let mut reader = BufReader::new(f);
    let table = _read_csv(&mut reader, geometry_column_name)?;
    Ok(GeoTable(table))
}
