import type { Property, SearchQuery } from '../types/real-estate.js';
import { zillowService } from '../services/zillow-service.js';
import { DataSourceError, logError, formatUserError } from '../utils/errors.js';
/**
* Property Search Tool - Now with real Zillow integration
*
* Searches for properties using Zillow API via RapidAPI.
* Falls back to mock data if API is unavailable (for development).
*/
export async function searchProperties(query: SearchQuery): Promise<Property[]> {
console.log(`Searching for properties: ${query.location}`);
try {
// Try real Zillow API first
const properties = await zillowService.searchProperties(query);
if (properties.length > 0) {
console.log(`Found ${properties.length} properties from Zillow API`);
return properties;
}
// If no results, could be legitimate (no properties in area)
console.log('No properties found from Zillow API');
return [];
} catch (error) {
logError(error as Error, 'Property Search');
// If it's a data source error and we have fallback available, use mock data for development
if (error instanceof DataSourceError && error.fallbackAvailable) {
console.warn('Zillow API unavailable, using fallback mock data for development');
return getFallbackMockData(query);
}
// For other errors, throw formatted user error
throw new Error(formatUserError(error as Error));
}
}
/**
* Fallback mock data for development when APIs are unavailable
* This should be removed in production
*/
function getFallbackMockData(query: SearchQuery): Property[] {
console.warn('WARNING: Using mock data. Configure RAPIDAPI_KEY for real data.');
const mockProperties: Property[] = [
{
id: 'fallback-1',
address: {
street: '123 Example St',
city: query.location.split(',')[0] || 'Sample City',
state: 'CA',
postalCode: '90210',
latitude: 34.0522,
longitude: -118.2437,
country: 'US'
},
price: query.minPrice || 500000,
beds: query.minBeds || 3,
baths: 2,
sqft: query.minSqft || 1500,
lotSizeSqft: 6000,
propertyType: query.propertyType || 'single_family',
yearBuilt: 2015,
url: 'https://www.zillow.com/homedetails/fallback-property',
source: 'zillow',
mlsId: 'FALLBACK123456',
daysOnMarket: 15,
hoaMonthly: 250,
taxesAnnual: 6000,
estimate: 520000,
description: 'FALLBACK DATA: Beautiful single family home with modern amenities. Configure your API keys for real data.',
photos: ['https://via.placeholder.com/400x300?text=Configure+API+Key'],
features: ['Fallback Data', 'Configure API Key', 'Real Data Available'],
listingStatus: 'active'
}
];
// Apply filters
return mockProperties.filter(p => {
if (query.maxPrice && p.price && p.price > query.maxPrice) return false;
if (query.maxSqft && p.sqft && p.sqft > query.maxSqft) return false;
return true;
});
}
/**
* Example of how to integrate with a real API:
*
* ```typescript
* import axios from 'axios';
*
* export async function searchPropertiesReal(query: SearchQuery): Promise<Property[]> {
* const response = await axios.get('https://api.example.com/properties', {
* params: {
* location: query.location,
* minPrice: query.minPrice,
* maxPrice: query.maxPrice,
* // ... other parameters
* },
* headers: {
* 'X-API-Key': process.env.REAL_ESTATE_API_KEY
* }
* });
*
* // Transform API response to our Property interface
* return response.data.results.map(transformToProperty);
* }
* ```
*/