interface Amenity {
    _id: string;
    name: string;
    icon: string;
    description: string;
  }
  interface PropertyTag {
    _id: string;
    name: string;
  }
  interface ApiProperty {
    _id: string;
    title: string;
    description: string;
    propertyType: { _id: string; name: string };
    subcategory?: { _id: string; name: string };
    listingType: string;
    status: string;
    reraId?: string;
    configuration?: { _id: string; name: string };
    project?: { _id: string; projectName: string };
    propertyTags?: PropertyTag[];
    carpetArea?: number;
    builtUpArea?: number;
    superBuiltUpArea?: number;
    bedrooms?: number | null;
    bathrooms?: number | null;
    balconies?: number | null;
    totalFloors?: number | null;
    floorNumber?: number | null;
    carParking?: string;
    furnishingType?: string;
    ageOfProperty?: string;
    parking?: string;
    address?: string;
    locality?: { _id?: string; name: string; pinCode?: number[]; city?: string };
    city?: { _id: string; name: string };
    state?: string;
    pincode?: number;
    facing?: string;
    latitude?: number;
    longitude?: number;
    ownerName?: string;
    ownerContact?: string;
    ownerEmail?: string;
    amenities?: Amenity[];
    tags?: PropertyTag[];
    mediaUrls?: string[];
    price?: number;
    unitOfMeasurement?: string;
    ownershipType?: string;
    availability?: string;
    createdAt?: string;
    updatedAt?: string;
    [key: string]: any;
  }
  interface PropertyDetails {
    title: string;
    description: string;
    location: string;
    images: string[];
    availableFor: string;
    propertyType: string;
    subcategory: string;
    furnishing: string;
    area: string;
    bedrooms: string | number | undefined;
    bathrooms: string | number | undefined;
    balconies: string | number | undefined;
    age: string | undefined;
    price: string;
    amenities: { name: string; icon: string }[];
    facing?: string;
    floor?: number | null;
    totalFloors?: number | null;
    owner: {
      name?: string;
      contact?: string;
      email?: string;
    };
    parking?: string;
    reraId?: string;
    project?: string;
    propertyTags?: PropertyTag[];
    address?: string;
  }
  
  // ---------------------
  // Mapping function
  // ---------------------
  export function mapPropertyToDetailView(api: ApiProperty): PropertyDetails {
    return {
      title: api.title,
      description: api.description,
      location: [
        api.locality?.name,
        typeof api.city === "object" ? api.city?.name : undefined,
      ]
        .filter(Boolean)
        .join(", "),
      images: Array.isArray(api.mediaUrls) ? api.mediaUrls : [],
      availableFor: api.listingType?.toUpperCase() === "SELL"
        ? "For Sale"
        : api.listingType?.toUpperCase() === "RENT"
        ? "For Rent"
        : api.listingType || "",
      propertyType: api.propertyType?.name || "",
      subcategory: api.subcategory?.name || "",
      furnishing: api.furnishingType
        ? api.furnishingType.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())
        : "",
      area: api.superBuiltUpArea
        ? `${api.superBuiltUpArea} ${api.unitOfMeasurement || "sqft"}`
        : "-",
      bedrooms: api.configuration?.name || api.bedrooms || undefined,
      bathrooms: api.bathrooms ?? undefined,
      balconies: api.balconies ?? undefined,
      age: api.ageOfProperty ?? undefined,
      price:
        typeof api.price === "number" ? `₹${api.price.toLocaleString()}` : "-",
      amenities: (api.amenities ?? []).map((a) => ({
        name: a.name,
        icon: a.icon,
      })),
      facing: api.facing ? api.facing.replace(/_/g, " ") : undefined,
      floor: api.floorNumber,
      totalFloors: api.totalFloors,
      owner: {
        name: api.ownerName,
        contact: api.ownerContact,
        email: api.ownerEmail,
      },
      parking: api.parking,
      reraId: api.reraId,
      project: api.project?.projectName,
      propertyTags: api.propertyTags ?? [],
      address: api.address,
    };
  }