import type { BrandAdminListItem } from "../@types/brandRequests.js";

type BrandRow = {
  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 toBrandListItem = (
  row: BrandRow,
  media: MediaRow[],
  productCount: number,
): BrandAdminListItem => ({
  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 slugifyBrand = (name: string): string => {
  return name
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9\s-]/g, "")
    .replace(/\s+/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");
};
