import type { Standard, StandardRow } from "@/lib/api/types/standard.types";

export function mapStandardToRow(standard: Standard, index: number): StandardRow {
  return {
    sno:        index + 1,
    id:         standard.id,
    name:       standard.name  ?? "—",
    title:      standard.title ?? "—",
    created_at: standard.created_at,
    updated_at: standard.updated_at,
  };
}

export function mapStandardsApiResponse(standards: Standard[]): StandardRow[] {
  return standards.map((s, idx) => mapStandardToRow(s, idx));
}

export function mapColumnKeyToValue(key: string, row: StandardRow): any {
  const map: Record<string, any> = {
    name:       row.name,
    title:      row.title,
    created_at: row.created_at
      ? new Date(row.created_at).toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric" }).replace(/ /g, "-")
      : "—",
    updated_at: row.updated_at
      ? new Date(row.updated_at).toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric" }).replace(/ /g, "-")
      : "—",
  };
  return map[key] ?? row[key] ?? "—";
}

export function formatStandardForExport(row: StandardRow): Record<string, string | number> {
  return {
    "No.":       row.sno,
    "Name":      row.name,
    "Title":     row.title,
    "Created":   row.created_at ? new Date(row.created_at).toLocaleDateString("en-GB") : "",
  };
}
