// utils/decentHomeChecks.ts

export function getAllRoomData(rooms: Record<string, any>) {
  return [
    ...Object.entries(rooms).filter(([_, v]) => v?.room_info),
    ...(rooms.bedrooms || []).map((b: any, i: number) => [
      "Bedroom " + (i + 1),
      b,
    ]),
    ...(rooms.bathrooms || []).map((b: any, i: number) => [
      "Bathroom " + (i + 1),
      b,
    ]),
  ];
}

export function getRoomsWithDamp(allRoomData: [string, any][]) {
  return allRoomData.filter(
    ([, room]: any) =>
      room.room_info?.ventilation_info
        ?.are_there_any_visible_or_reported_signs_of_damp_mould_or_excessive_condensation_within_the_room
  );
}

export function getRoomsWithDefects(allRoomData: [string, any][]) {
  return allRoomData.filter(
    ([, room]: any) => room.room_info?.does_the_room_have_any_defects === "Yes"
  );
}

export function getRoomsWithBadWindows(allRoomData: [string, any][]) {
  return allRoomData.filter(([, room]: any) => {
    const wi = room.room_info?.windows_info;
    return (
      wi?.does_the_room_have_any_windows &&
      wi.condition_of_the_windows !== "Good condition"
    );
  });
}

export function areAllWindowsOk(allRoomData: [string, any][]) {
  return allRoomData.every(([, room]: any) => {
    const wi = room.room_info?.windows_info;
    return wi?.does_the_room_have_any_windows
      ? wi?.condition_of_the_windows === "Good condition"
      : true;
  });
}

export function getElevationsWithIssues(conditionReport: any): [string, any][] {
  const frontElevation =
    conditionReport.access_and_elevations?.external_elevation_front
      ?.external_elevation;
  const applyFrontDefaults = (
    label: string,
    elevationKey: string
  ): [string, any] => {
    const section = conditionReport.access_and_elevations?.[elevationKey];
    if (section?.do_all_answers_for_the_front_elevation_apply_to_this_wall) {
      return [label, frontElevation];
    }
    return [label, section?.external_elevation ?? null];
  };

  const allElevations: [string, any][] = [
    ["Front Elevation", frontElevation],
    applyFrontDefaults("Back Elevation", "external_elevation_back"),
    applyFrontDefaults("Gable One", "external_elevation_gable_one"),
    applyFrontDefaults("Gable Two", "external_elevation_gable_two"),
  ];

  return allElevations.filter(
    ([_, elevation]) =>
      elevation &&
      (elevation.does_any_structural_defect_need_resolving_before_retrofit ===
        true ||
        elevation.are_there_any_signs_of_water_penetration_caused_by_failed_rainwater_goods_or_pipework ===
          true ||
        elevation.are_there_any_visible_signs_of_movement === true ||
        elevation.are_there_any_visible_signs_of_cracking_to_the_existing_external_finish ===
          true)
  );
}

export function hasSufficientSpace(
  conditionReport: any,
  totalFloorArea: number,
  allRoomData: [string, any][]
) {
  const totalOccupants =
    conditionReport.occupant_info?.total_number_of_occupants ?? 0;
  const totalAdults = conditionReport.occupant_info?.no_of_adult_occupants ?? 0;
  const totalChildren =
    conditionReport.occupant_info?.no_of_child_occupants ?? 0;
  const occupantsToUse = Math.max(totalAdults + totalChildren, totalOccupants);

  console.log("totalOccupants", totalOccupants);
  console.log("occupantsToUse", occupantsToUse);

  const numberOfBedrooms = allRoomData.filter(([label]) =>
    label.includes("Bedroom")
  ).length;

  const getRecommendedOccupants = (bedrooms: number) => {
    if (bedrooms <= 0) return 0;
    if (bedrooms === 1) return 2;
    if (bedrooms === 2) return 4;
    if (bedrooms === 3) return 6;
    return 7;
  };

  const maxOccupants = getRecommendedOccupants(numberOfBedrooms);
  const areaPerPerson = totalFloorArea / occupantsToUse;
  const isSufficient = occupantsToUse <= maxOccupants && areaPerPerson >= 20;

  return { isSufficient, occupantsToUse, numberOfBedrooms, areaPerPerson };
}

export function hasEfficientHeatingSystem(
  heatingRating: number | null
): boolean {
  return heatingRating != null && heatingRating >= 3;
}

export function isInsulationAdequate(
  roofRating: number | null,
  wallRating: number | null
): boolean {
  return (
    (roofRating != null && roofRating >= 3) ||
    (wallRating != null && wallRating >= 3)
  );
}

export function meetsSapThreshold(currentSapPoints: number): boolean {
  return currentSapPoints >= 35;
}
