import type { FrameShapeAdminListItem } from "../@types/frameShapeRequests.js";

type FrameShapeRow = {
  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;
};

type ProductCountRow = {
  count: number;
};

export const toFrameShapeListItem = (
  row: FrameShapeRow,
  media: MediaRow[],
  productCount: number,
): FrameShapeAdminListItem => ({
  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 slugifyFrameShape = (name: string): string => {
  return name
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9\s-]/g, "")
    .replace(/\s+/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");
};
