import type { Post, Project, Product, TimelineEvent, UsesItem, SiteSettings, Tag } from "@prisma/client";
import type { PostWithTags } from "@/lib/db/posts";
import type { ProjectWithArrays } from "@/lib/db/projects";

export type PostCategory =
  | "founder-notes"
  | "marketing-branding"
  | "product-thinking"
  | "tech-builds"
  | "business-experiments"
  | "systems-productivity";

export type ProjectType = "product" | "brand-system" | "open-source" | "creative-work";

export type { Post, Project, Product, TimelineEvent, UsesItem, SiteSettings, Tag, PostWithTags, ProjectWithArrays };

export function formatDate(dateStr: string | Date, locale: string): string {
  if (!dateStr) return "";
  try {
    const d = typeof dateStr === "string" ? new Date(dateStr) : dateStr;
    return new Intl.DateTimeFormat(locale === "fa" ? "fa-IR" : "en-US", {
      year: "numeric",
      month: "long",
      day: "numeric",
    }).format(d);
  } catch {
    return String(dateStr);
  }
}

export function formatPersianDate(date: string | Date): string {
  if (!date) return "";
  try {
    const d = typeof date === "string" ? new Date(date) : date;
    return new Intl.DateTimeFormat("fa-IR", { year: "numeric", month: "long", day: "numeric" }).format(d);
  } catch {
    return String(date);
  }
}

// Flat types used by frontend components (Sanity image objects replaced by URL strings)
export interface FlatPost {
  id: string;
  title: string;
  slug: string;
  locale: string;
  category: string;
  excerpt: string | null;
  publishedAt: Date;
  featured: boolean;
  coverImage: string | null;
  body: string | null;
  tags: { id: string; title: string; slug: string }[];
  seoTitle: string | null;
  seoDesc: string | null;
}

export interface FlatProject {
  id: string;
  title: string;
  slug: string;
  locale: string;
  projectType: string;
  description: string | null;
  coverImage: string | null;
  gallery: string[];
  techStack: string[];
  toolsUsed: string[];
  liveUrl: string | null;
  githubUrl: string | null;
  featured: boolean;
  body: string | null;
  seoTitle: string | null;
  seoDesc: string | null;
}

export function slugify(text: string): string {
  return text
    .toLowerCase()
    .trim()
    .replace(/[\s_]+/g, "-")
    .replace(/[^\w\u0600-\u06FF-]/g, "")
    .replace(/-+/g, "-");
}
