import type { DealDetailResponse, SimilarDealResponse } from "../../api/deal/types"
import type { DealDetail } from "../../models/deal/DealDetail"
import type { SimilarDeal } from "../../models/Shared/SimilarDeal"

export function mapDealDetailResponseToDealDetail(api: DealDetailResponse): DealDetail {
const similarDeals: SimilarDeal[] = (api.similarDeals ?? []).map((d) => ({
  id: d.id,
  title: d.title,
  price: d.price ?? null,                 // ✅ undefined yok, null var
  url: d.url ?? undefined,                // url optional ise undefined OK
  score: d.score ?? 0,
  createdAt: d.createdAt,
  sellerName: d.sellerName ?? "Bilinmiyor",
  imageUrl: d.imageUrl || "/placeholder.png",
}))


  return {
    id: api.id,
    title: api.title,
    description: api.description,

    url: api.url ?? undefined,
    price: api.price ?? undefined,
    score: api.score,
    commentsCount: api.commentsCount,

    status: api.status,
    saleType: api.saleType,
    affiliateType: api.affiliateType,

    createdAt: api.createdAt,
    updatedAt: api.updatedAt,

    user: api.user,
    seller: {
      name: api.seller.name,
      url: api.seller.url,
    },

    images: api.images.map((img) => ({
      imageUrl: img.imageUrl,
      order: img.order,
    })),

    comments: api.comments.map((c) => ({
      id: c.id,
      text: c.text,
      createdAt: c.createdAt,
      user: c.user,
    })),

    breadcrumb: (api.breadcrumb ?? []).map((b) => ({
      id: b.id,
      name: b.name,
      slug: b.slug,
    })),

    notice: api.notice ?? null,

    // ✅ yeni
    similarDeals,
  }
}
