"""Parse ars_25.csv into StatuteRecord objects."""

import csv
from pathlib import Path

from ars25.models.statute import StatuteRecord


def load_csv(csv_path: str | Path) -> list[StatuteRecord]:
    """Parse the ARS Title 25 CSV file.

    Handles UTF-8 BOM encoding and returns typed StatuteRecord objects.
    """
    path = Path(csv_path)
    records: list[StatuteRecord] = []

    with open(path, encoding="utf-8-sig", newline="") as f:
        reader = csv.DictReader(f)
        for row in reader:
            if not row.get("ars_section", "").strip():
                continue
            records.append(StatuteRecord(
                chapter_number=int(row["chapter_number"]),
                chapter_title=row["chapter_title"].strip(),
                article_number=int(row["article_number"]),
                article_title=row["article_title"].strip(),
                section=row["ars_section"].strip(),
                title=row["ars_title"].strip(),
                url=row["ars_url"].strip(),
            ))

    return records
