import { shopProducts, type ShopProduct } from "@/data/shop";
import { shopCategories, shopTags } from "@/data/shop";

export type ProductDetailGallerySlide = {
  id: string;
  thumbSrc: string;
  largeSrc: string;
  alt: string;
};

export type ProductDetailBullet = {
  id: string;
  text: string;
};

export type ProductDetailContent = {
  product: ShopProduct;
  gallery: ProductDetailGallerySlide[];
  paragraphs: string[];
  bullets: ProductDetailBullet[];
  meta: {
    sku: string;
    categoriesLabel: string;
    tagsLabel: string;
    reviewsText: string;
  };
};

export const PRODUCT_DETAIL_DEFAULT_ID = "business-school-bag";

const defaultBullets: ProductDetailBullet[] = [
  { id: "b1", text: "Premium Quality Materials" },
  { id: "b2", text: "Eco-Conscious Manufacturing" },
  { id: "b3", text: "Easy Returns & Support" },
];

const defaultParagraphs = [
  "Designed for everyday versatility, this product combines modern style with practical comfort for day-to-day use.",
  "With durable materials and a cushioned feel, it is a dependable pick whether you are commuting, traveling, or shopping.",
];

function toSku(productId: string) {
  return `SKU-${productId.replace(/[^a-zA-Z0-9]/g, "").toUpperCase().slice(0, 8)}`;
}

function buildFallbackGallery(product: ShopProduct): ProductDetailGallerySlide[] {
  return Array.from({ length: 4 }, (_, index) => ({
    id: `${product.id}-${index + 1}`,
    thumbSrc: product.imageSrc,
    largeSrc: product.imageSrc,
    alt: `${product.imageAlt} view ${index + 1}`,
  }));
}

function toCategoryLabel(product: ShopProduct) {
  const labels = product.categoryIds
    .map((id) => shopCategories.find((c) => c.id === id)?.label)
    .filter((v): v is string => Boolean(v));
  return labels.length > 0 ? labels.join(", ") : "Uncategorized";
}

function toTagLabel(product: ShopProduct) {
  const labels = product.tagIds
    .map((id) => shopTags.find((t) => t.id === id)?.label)
    .filter((v): v is string => Boolean(v));
  return labels.length > 0 ? labels.join(", ") : "Shopping";
}

function getGallery(product: ShopProduct) {
  if (product.detailGallery && product.detailGallery.length > 0) {
    return product.detailGallery.map((img, index) => ({
      id: `${product.id}-g-${index + 1}`,
      thumbSrc: img.thumbSrc,
      largeSrc: img.largeSrc,
      alt: img.alt,
    }));
  }
  return buildFallbackGallery(product);
}

function toDetailContent(product: ShopProduct): ProductDetailContent {
  return {
    product,
    gallery: getGallery(product),
    paragraphs: defaultParagraphs,
    bullets: defaultBullets,
    meta: {
      sku: toSku(product.id),
      categoriesLabel: toCategoryLabel(product),
      tagsLabel: toTagLabel(product),
      reviewsText: `(${product.rating} customer review${product.rating === 1 ? "" : "s"})`,
    },
  };
}

export function getProductDetailContentById(
  productId: string,
): ProductDetailContent | null {
  const product = shopProducts.find((p) => p.id === productId);
  if (!product) return null;
  return toDetailContent(product);
}
