import type { ProductTypeAdminListItem } from "../@types/productTypeRequests.js";

type ProductTypeRow = {
  id: number;
  name: string;
  slug: string;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
};

type MediaRow = {
  id: number;
  url: string;
  altText: string | null;
  isPrimary: boolean;
  sortOrder: number;
};

export const toProductTypeListItem = (
  row: ProductTypeRow,
  media: MediaRow[],
  productCount: number,
): ProductTypeAdminListItem => ({
  id: row.id,
  name: row.name,
  slug: row.slug,
  isActive: row.isActive,
  media: media.map((m) => ({
    id: m.id,
    url: m.url,
    altText: m.altText,
    isPrimary: m.isPrimary,
    sortOrder: m.sortOrder,
  })),
  productCount,
  createdAt: row.createdAt,
  updatedAt: row.updatedAt,
});

export const slugifyProductType = (name: string): string => {
  return name
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9\s-]/g, "")
    .replace(/\s+/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");
};
