// API-projection voor Idea — converteert Prisma-row naar het externe contract.
// Belangrijk: status wordt naar lowercase API-string vertaald (zelfde patroon
// als TaskStatus / StoryStatus / PbiStatus elders in de codebase).

import { ideaStatusToApi } from '@/lib/idea-status'

import type { Idea, IdeaStatus, Product } from '@prisma/client'

type IdeaWithProduct = Idea & {
  product: Pick<Product, 'id' | 'name' | 'repo_url'> | null
  pbi?: { id: string; code: string; title: string } | null
  secondary_products?: { id: string; product_id: string; product: { id: string; name: string } }[]
}

export interface IdeaDto {
  id: string
  code: string
  title: string
  description: string | null
  status: ReturnType<typeof ideaStatusToApi>
  product_id: string | null
  product: { id: string; name: string; repo_url: string | null } | null
  pbi_id: string | null
  pbi?: { id: string; code: string; title: string } | null
  secondary_products: { id: string; product_id: string; product: { id: string; name: string } }[]
  archived: boolean
  has_grill_md: boolean
  has_plan_md: boolean
  created_at: string
  updated_at: string
}

export function ideaToDto(idea: IdeaWithProduct & { status: IdeaStatus }): IdeaDto {
  return {
    id: idea.id,
    code: idea.code,
    title: idea.title,
    description: idea.description,
    status: ideaStatusToApi(idea.status),
    product_id: idea.product_id,
    product: idea.product,
    pbi_id: idea.pbi_id,
    pbi: idea.pbi ?? null,
    secondary_products: idea.secondary_products ?? [],
    archived: idea.archived,
    // Geen md-content in lijst-payloads (kan groot zijn) — enkel een vlag.
    has_grill_md: idea.grill_md !== null,
    has_plan_md: idea.plan_md !== null,
    created_at: idea.created_at.toISOString(),
    updated_at: idea.updated_at.toISOString(),
  }
}
