import csv
from decimal import Decimal
from typing import List
from ledger_action import LedgerAction


def parse_row(row: dict[str, str]) -> LedgerAction:
    return LedgerAction(
        type=row["type"],
        asset=row["asset"],
        amount=Decimal(row["amount"]),
        fee=Decimal(row.get("fee", "0")),
        refid=row.get("refid", ""),
        timestamp=row["time"],
    )


def read_ledger(csv_path: str) -> List[LedgerAction]:
    with open(csv_path, "r") as file:
        reader = csv.DictReader(file)
        return list(map(parse_row, reader))