from __future__ import annotations
from pathlib import Path
import polars as pl
import duckdb


def read_csv(
    obj: str | Path | pl.DataFrame,
    *,
    try_parse_dates: bool = False,
) -> pl.DataFrame:
    """
    Reads a CSV from path or passthrough DataFrame.
    """
    if isinstance(obj, pl.DataFrame):
        return obj.clone()

    if isinstance(obj, (str, Path)):
        return pl.read_csv(
            obj,
            try_parse_dates=try_parse_dates,
        )

    raise TypeError(f"Unsupported CSV input: {type(obj)}")


def read_parquet(path: str | Path) -> pl.DataFrame:
    """
    Reads a Parquet file.
    """
    return pl.read_parquet(path)


def read_duckdb_table(
    conn: duckdb.DuckDBPyConnection,
    table: str,
) -> pl.DataFrame:
    """
    Reads a DuckDB table into Polars.
    """
    return pl.from_arrow(
        conn.execute(f"SELECT * FROM {table}").arrow()
    )