import { db } from "@/lib/db";
import {
  category,
  mediaAsset,
  mediaUsage,
  portfolioAsset,
  portfolioProject,
  portfolioSection,
} from "@/lib/db/schema";
import { MediaKind, MediaSource, MediaUsageType, PortfolioSectionType } from "@/lib/db/enums";

let counter = 0;
function uniq(prefix: string) {
  counter += 1;
  return `${prefix}-${counter}`;
}

export async function createCategory(overrides: Record<string, unknown> = {}) {
  const [row] = await db
    .insert(category)
    .values({
      slug: (overrides.slug as string) ?? uniq("cat"),
      nameAr: "الاسم",
      nameEn: "Name",
      nameDe: "Name",
      descriptionAr: "وصف",
      descriptionEn: "Description",
      descriptionDe: "Beschreibung",
      sortOrder: 0,
      isActive: true,
      ...overrides,
    } as typeof category.$inferInsert)
    .returning();

  return row;
}

export async function createProject(overrides: Record<string, unknown> = {}) {
  const categoryId = (overrides.categoryId as string) ?? (await createCategory()).id;
  const isPublished = (overrides.isPublished as boolean) ?? true;
  const slug = (overrides.slug as string) ?? uniq("proj");
  const [row] = await db
    .insert(portfolioProject)
    .values({
      categoryId,
      slug,
      viewMode: "GRID",
      titleAr: "عنوان",
      titleEn: "Title",
      titleDe: "Titel",
      summaryAr: "ملخص",
      summaryEn: "Summary",
      summaryDe: "Zusammenfassung",
      clientName: "Client",
      projectYear: 2025,
      serviceLabelAr: "خدمة",
      serviceLabelEn: "Service",
      serviceLabelDe: "Service",
      isFeatured: false,
      sortOrder: 0,
      ...overrides,
      isPublished,
      publishedAt: isPublished ? new Date() : null,
    } as typeof portfolioProject.$inferInsert)
    .returning();

  return row;
}

export async function createSection(projectId: string, overrides: Record<string, unknown> = {}) {
  const [row] = await db
    .insert(portfolioSection)
    .values({
      projectId,
      type: PortfolioSectionType.RICH_TEXT,
      titleAr: "ع",
      titleEn: "t",
      titleDe: "t",
      bodyAr: "ب",
      bodyEn: "b",
      bodyDe: "b",
      sortOrder: 0,
      ...overrides,
    } as typeof portfolioSection.$inferInsert)
    .returning();

  return row;
}

export async function createAsset(projectId: string, overrides: Record<string, unknown> = {}) {
  const [row] = await db
    .insert(portfolioAsset)
    .values({
      projectId,
      kind: "IMAGE",
      filePath: "/uploads/media/assets/x.svg",
      altAr: "ع",
      altEn: "a",
      altDe: "a",
      sortOrder: 0,
      ...overrides,
    } as typeof portfolioAsset.$inferInsert)
    .returning();

  return row;
}

export async function createMediaAsset(overrides: Record<string, unknown> = {}) {
  const [row] = await db
    .insert(mediaAsset)
    .values({
      source: MediaSource.EXTERNAL,
      kind: MediaKind.IMAGE,
      url: (overrides.url as string) ?? `https://cdn.example.com/${uniq("img")}.png`,
      fileName: "img.png",
      label: "Image",
      ...overrides,
    } as typeof mediaAsset.$inferInsert)
    .returning();

  return row;
}

export async function createMediaUsage(assetId: string, overrides: Record<string, unknown> = {}) {
  const [row] = await db
    .insert(mediaUsage)
    .values({
      assetId,
      usageType: MediaUsageType.GENERIC,
      entityType: "test-entity",
      entityId: "e1",
      fieldKey: uniq("field"),
      ...overrides,
    } as typeof mediaUsage.$inferInsert)
    .returning();

  return row;
}
