import { getUser } from "../api/user/getUser"
import type { PublicUserSummary, Deal, Comment } from "../models"

export type UserProfile = {
  user: PublicUserSummary
  deals: Deal[]
  comments: Comment[]
}

export async function fetchUserProfile(userName: string): Promise<UserProfile> {
  const data = await getUser(userName)

  const user: PublicUserSummary = {
    username: data.user.username,
    avatarUrl: data.user.avatarUrl,
    createdAt: data.user.createdAt,
  }

  const deals: Deal[] = data.deals.map((d: any) => ({
    id: d.id,
    title: d.title,
    price: d.price,
    store: d.store,
    score: d.score,
    createdAt: d.createdAt,
    images:
      d.images?.map((img: any) => ({
        imageUrl: img.imageUrl,
        order: img.order,
      })) || [],
  }))

  const comments: Comment[] = data.comments.map((c: any) => ({
    id: c.id,
    text: c.text,
    createdAt: c.createdAt,
    deal: { title: c.deal?.title || "Bilinmeyen paylaşım" },
  }))

  return { user, deals, comments }
}
