"""
Recipe: thin wrapper around SFDC enrich to match recipe naming conventions.
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional, Tuple

from g_gremlin import sfdc as sfdc_helpers


@dataclass
class EnrichFromSfdcResult:
    rows_written: int
    fields: List[str]


def enrich_from_sfdc(
    csv_path: Path,
    id_column: str,
    sobject: str,
    fields: List[Tuple[str, Optional[str]]],
    output_path: Path,
    org_alias: Optional[str] = None,
) -> EnrichFromSfdcResult:
    """
    Enrich a CSV using SFDC fields (wrapper around sfdc.enrich_csv_with_sfdc).
    """
    if not fields:
        raise ValueError("At least one field is required for enrich-from-sfdc.")

    sfdc_helpers.enrich_csv_with_sfdc(
        csv_path, id_column, sobject, fields, output_path, org_alias
    )

    rows_written = 0
    with output_path.open("r", encoding="utf-8") as handle:
        next(handle, None)  # skip header
        for rows_written, _ in enumerate(handle, start=1):
            pass
    return EnrichFromSfdcResult(rows_written=rows_written, fields=[f[0] for f in fields])
