import {
  getPropertyMeta,
  getDocument,
  getEnergyAssessmentFromS3,
} from "../utils";
import DecentHomesSummary from "./DecentHomesSummary";

export default async function DecentHomesPage(props: {
  params: Promise<{ slug: string; propertyId: string }>;
}) {
  const params = await props.params;
  const propertyMeta = await getPropertyMeta(params.propertyId);

  const decentHomesSummary = await getDocument({
    uprn: String(propertyMeta.uprn),
    documentType: "DECENT_HOMES_SUMMARY",
  });

  const decentPropertyMeta = await getDocument({
    uprn: String(propertyMeta.uprn),
    documentType: "DECENT_HOMES_PROPERTY_META",
  });

  if (
    Object.keys(decentHomesSummary).length === 0 ||
    Object.keys(decentPropertyMeta).length === 0 ||
    !decentHomesSummary.s3JsonUri ||
    !decentPropertyMeta.s3JsonUri
  ) {
    throw new Error(
      `Decent Homes data is missing for uprn ${propertyMeta.uprn}`,
    );
  }

  const decentHomesMeta = await getEnergyAssessmentFromS3(
    decentPropertyMeta.s3JsonUri,
  );
  const decentHomes = await getEnergyAssessmentFromS3(
    decentHomesSummary.s3JsonUri,
  );

  return (
    <DecentHomesSummary
      decentHomes={decentHomes}
      decentHomesMeta={decentHomesMeta}
    />
  );
}
