import { placeholderSrc } from "@/lib/placeholder";

export const TEMPLATE_CATEGORIES = [
  "All",
  "Video",
  "Image",
  "Social",
  "Business",
] as const;

export const TEMPLATE_STYLES = ["Minimal", "Bold", "Elegant"] as const;

export const TEMPLATE_COLORS = [
  { id: "blue", label: "Blue", className: "bg-primary-600" },
  { id: "violet", label: "Violet", className: "bg-violet-600" },
  { id: "rose", label: "Rose", className: "bg-rose-500" },
  { id: "emerald", label: "Emerald", className: "bg-emerald-500" },
  { id: "amber", label: "Amber", className: "bg-amber-500" },
  { id: "slate", label: "Slate", className: "bg-slate-600" },
] as const;

export type TemplateCategoryFilter = (typeof TEMPLATE_CATEGORIES)[number];
export type TemplateStyle = (typeof TEMPLATE_STYLES)[number];
export type TemplateColorId = (typeof TEMPLATE_COLORS)[number]["id"];

export type TemplateCatalogCategory = Exclude<TemplateCategoryFilter, "All">;

export interface CatalogTemplate {
  id: string;
  name: string;
  category: TemplateCatalogCategory;
  style: TemplateStyle;
  color: TemplateColorId;
}

const namesByCategory: Record<TemplateCatalogCategory, string[]> = {
  Video: [
    "Promo Reel",
    "Product Launch",
    "Brand Story",
    "Tutorial Intro",
    "Event Recap",
    "Testimonial",
  ],
  Image: [
    "Hero Banner",
    "Catalog Spread",
    "Poster",
    "Flyer",
    "Lookbook",
    "Quote Card",
  ],
  Social: [
    "Instagram Carousel",
    "TikTok Hook",
    "Story Highlight",
    "LinkedIn Post",
    "Pinterest Pin",
    "Thread Cover",
  ],
  Business: [
    "Pitch Deck",
    "One Pager",
    "Report Cover",
    "Newsletter",
    "Invoice",
    "Meeting Slide",
  ],
};

function buildCatalog(): CatalogTemplate[] {
  const categories: TemplateCatalogCategory[] = [
    "Video",
    "Image",
    "Social",
    "Business",
  ];
  const styles: TemplateStyle[] = ["Minimal", "Bold", "Elegant"];
  const colors: TemplateColorId[] = [
    "blue",
    "violet",
    "rose",
    "emerald",
    "amber",
    "slate",
  ];
  const items: CatalogTemplate[] = [];
  let index = 0;

  for (const category of categories) {
    for (const baseName of namesByCategory[category]) {
      for (let variant = 0; variant < 2; variant += 1) {
        const style = styles[index % styles.length];
        const color = colors[index % colors.length];
        items.push({
          id: `tpl-${category.toLowerCase()}-${index}`,
          name: `${baseName}${variant > 0 ? ` ${variant + 1}` : ""}`.trim(),
          category,
          style,
          color,
        });
        index += 1;
      }
    }
  }

  return items;
}

export const TEMPLATES_CATALOG = buildCatalog();

export const TEMPLATES_PAGE_SIZE = 24;

export function getTemplateImageSrc(id: string): string {
  return placeholderSrc(id, 480, 360);
}

export interface TemplateFilters {
  search: string;
  category: TemplateCategoryFilter;
  style: TemplateStyle | null;
  color: TemplateColorId | null;
}

export function filterCatalog(
  templates: CatalogTemplate[],
  filters: TemplateFilters
): CatalogTemplate[] {
  const query = filters.search.trim().toLowerCase();

  return templates.filter((template) => {
    if (filters.category !== "All" && template.category !== filters.category) {
      return false;
    }
    if (filters.style && template.style !== filters.style) {
      return false;
    }
    if (filters.color && template.color !== filters.color) {
      return false;
    }
    if (query && !template.name.toLowerCase().includes(query)) {
      return false;
    }
    return true;
  });
}
