import {
  aggregateAcrossTeams,
  formatAggregationResult,
  loopGet,
} from "../lib/loop-api.js";

// ─── Loop API V2: /property/residential/{dept}/{id} ────────────
// Fields: id, refId, address_DisplayAddress, address_HouseNameOrNumber,
//   address_HouseSecondaryNameOrNumber, address_Street, address_Locality,
//   address_Town, address_Postcode, status, propertyType, dateCreated,
//   dateInstructed, dateLaunched, dateExchanged, dateCompleted,
//   dateWithdrawn, shortDescription, fullDescription, bathrooms,
//   bedrooms, receptionRooms, price, priceQualifier, latitude,
//   longitude, parking, outsideSpace, councilTaxBand, floorArea,
//   tenure, serviceCharge, groundRent, features[], images[],
//   marketingFlag_*, creatingAgentId
// ────────────────────────────────────────────────────────────────
interface LoopPropertyDetail {
  id: number;
  refId?: string;
  address_DisplayAddress?: string | null;
  address_HouseNameOrNumber?: string;
  address_HouseSecondaryNameOrNumber?: string;
  address_Street?: string;
  address_Locality?: string | null;
  address_Town?: string;
  address_Postcode?: string;
  status?: string;
  propertyType?: string;
  dateCreated?: string;
  dateInstructed?: string | null;
  dateLaunched?: string | null;
  dateExchanged?: string | null;
  dateCompleted?: string | null;
  dateWithdrawn?: string | null;
  shortDescription?: string;
  fullDescription?: string;
  bathrooms?: number;
  bedrooms?: number;
  receptionRooms?: number;
  price?: number;
  priceQualifier?: string;
  latitude?: number;
  longitude?: number;
  parking?: string;
  outsideSpace?: string;
  councilTaxBand?: string;
  floorArea?: number;
  tenure?: string;
  serviceCharge?: number;
  groundRent?: number;
  features?: string[];
  images?: { url: string; mimeType?: string }[];
  creatingAgentId?: string;
  isDirectViewingsEnabled?: boolean;
  [key: string]: unknown;
}

type Department = "sales" | "lettings";

export async function propertyDetail(params: {
  accountId: string;
  propertyId: number;
  department: Department;
  previewHash?: number;
  teamName?: string;
}): Promise<string> {
  const { accountId, propertyId, department, previewHash, teamName } = params;

  const result = await aggregateAcrossTeams<LoopPropertyDetail>(
    accountId,
    "properties",
    "loop-property-detail",
    async (apiKey, team) => {
      const path = previewHash
        ? `/property/residential/${department}/${propertyId}/preview/${previewHash}`
        : `/property/residential/${department}/${propertyId}`;
      const data = await loopGet<LoopPropertyDetail>(apiKey, path, "loop-property-detail", team);
      if (data && typeof data === "object" && !Array.isArray(data)) {
        return [data];
      }
      return [];
    },
    { teamName }
  );

  return formatAggregationResult(
    result,
    (p) => {
      // Build display address from components
      const addr = p.address_DisplayAddress
        ?? ([
          p.address_HouseSecondaryNameOrNumber,
          p.address_HouseNameOrNumber,
          p.address_Street,
          p.address_Town,
          p.address_Postcode,
        ].filter(Boolean).join(", ") || "Unknown address");

      const lines = [`**${addr}** [ID: ${p.id}]`];
      if (p.price != null) {
        const qualifier = p.priceQualifier && p.priceQualifier !== "none" ? ` (${p.priceQualifier})` : "";
        lines.push(`Price: £${p.price.toLocaleString("en-GB")}${qualifier}`);
      }
      if (p.status) lines.push(`Status: ${p.status}`);
      if (p.propertyType) lines.push(`Type: ${p.propertyType}`);
      if (p.bedrooms != null) lines.push(`Bedrooms: ${p.bedrooms}`);
      if (p.bathrooms != null) lines.push(`Bathrooms: ${p.bathrooms}`);
      if (p.receptionRooms != null) lines.push(`Reception rooms: ${p.receptionRooms}`);
      if (p.floorArea) lines.push(`Floor area: ${p.floorArea} sq ft`);
      if (p.tenure && p.tenure !== "None") lines.push(`Tenure: ${p.tenure}`);
      if (p.councilTaxBand) lines.push(`Council Tax: Band ${p.councilTaxBand}`);
      if (p.parking) lines.push(`Parking: ${p.parking}`);
      if (p.outsideSpace) lines.push(`Outside: ${p.outsideSpace}`);
      if (p.serviceCharge) lines.push(`Service charge: £${p.serviceCharge}`);
      if (p.groundRent) lines.push(`Ground rent: £${p.groundRent}`);

      // Key dates
      const dates: string[] = [];
      if (p.dateInstructed) dates.push(`Instructed: ${p.dateInstructed.split("T")[0]}`);
      if (p.dateLaunched) dates.push(`Launched: ${p.dateLaunched.split("T")[0]}`);
      if (p.dateExchanged) dates.push(`Exchanged: ${p.dateExchanged.split("T")[0]}`);
      if (p.dateCompleted) dates.push(`Completed: ${p.dateCompleted.split("T")[0]}`);
      if (p.dateWithdrawn) dates.push(`Withdrawn: ${p.dateWithdrawn.split("T")[0]}`);
      if (dates.length) lines.push(dates.join(" | "));

      if (p.features?.length) {
        lines.push(`Features: ${p.features.join(", ")}`);
      }

      if (p.images?.length) {
        lines.push(`Images: ${p.images.length} photo${p.images.length !== 1 ? "s" : ""}`);
      }

      // Short description — strip HTML tags for readability
      if (p.shortDescription) {
        const clean = p.shortDescription.replace(/<[^>]+>/g, "").trim();
        if (clean) lines.push(`\n${clean}`);
      }

      return lines.join("\n");
    },
    "property details"
  );
}
