import { loopPost, withTeamKey } from "../lib/loop-api.js";

type Department = "sales" | "lettings";
type RequestAction = "viewing" | "call-back" | "information";

// ─── Loop API V2: POST /property/residential/{dept}/{id}/{action}
// Actions: viewing, call-back, information
// Request body: { name?, email?, phone?, message? }
// Response: boolean success
// ────────────────────────────────────────────────────────────────
interface LoopBooleanResponse {
  success?: boolean;
  [key: string]: unknown;
}

export async function propertyRequest(params: {
  accountId: string;
  teamName: string;
  propertyId: number;
  department: Department;
  action: RequestAction;
  name?: string;
  email?: string;
  phone?: string;
  message?: string;
}): Promise<string> {
  const { accountId, teamName, propertyId, department, action, ...body } = params;

  const result = await withTeamKey<LoopBooleanResponse>(
    accountId,
    teamName,
    "properties",
    "loop-property-request",
    async (apiKey) => {
      const path = `/property/residential/${department}/${propertyId}/${action}`;
      return loopPost<LoopBooleanResponse>(apiKey, path, body, "loop-property-request", teamName);
    }
  );

  const actionLabel = action === "call-back" ? "callback" : action;
  return `${actionLabel} request submitted for property ${propertyId} (${department}) via team "${teamName}".`;
}
