import { notFound } from "next/navigation";

import { PortraitView } from "@/components/domains/portraits";
import { DashboardData, DashboardSchema } from "@/entities/dashboard/types";
import { http } from "@/shared/api/httpClient";
import { API_ROUTES } from "@/shared/constants/api-routes";

// Force dynamic to always get fresh data
export const dynamic = "force-dynamic";

export default async function PortraitPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;

  // Get fresh dashboard data without cache
  const dashboard = await http.get<DashboardData>(API_ROUTES.dashboard(), {
    cache: "no-store",
    schema: DashboardSchema,
  });

  const portrait = dashboard.partnerPortraits?.find(p => p._id === id);

  if (!portrait || portrait.status !== "done" || !portrait.imageUrl) {
    notFound();
  }

  return (
    <PortraitView
      id={portrait._id}
      title={portrait.title}
      imageUrl={portrait.imageUrl}
      result={portrait.result}
    />
  );
}
