import os
import csv
from datetime import datetime
from api.module import Module
from api.io_port import InPort, CSVTableType


def factory():
    m = Module("CSVWriter")

    inp = InPort("features", CSVTableType(columns=["name", "value"]))
    m.add_in_port(inp)

    def execute(inputs):
        data = inputs["features"]

        rows = []
        try:
            import pandas as pd
            if hasattr(data, "iterrows"):
                for _, row in data.iterrows():
                    rows.append({"name": row[0], "value": row[1]})
            else:
                rows = list(data)
        except Exception:
            rows = list(data)

        os.makedirs("results", exist_ok=True)
        ts = datetime.now().strftime("%Y%m%d_%H%M%S")
        path = os.path.join("results", f"features_{ts}.csv")

        with open(path, "w", newline="", encoding="utf-8") as f:
            w = csv.writer(f)
            w.writerow(["name", "value"])
            for r in rows:
                w.writerow([r["name"], r["value"]])

        return {
            "csv_path": path
        }

    m.execute = execute
    return m
