<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;

class ZillowController extends Controller
{
	
	public function getEstimateByAddress(Request $request)
	{
		
		$estimate = array(
			'amount' => 'Not Available',
			'last_updated' => 'Not Available',
		);
	
		$address = $request->address;
		
		if ($address) {
						
			$xml_property = simplexml_load_file('http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=' . env('APP_ZILLOW_API_KEY') .
							'&address=' . $address['address'] .
								'&citystatezip=' . $address['city'] . ' ' . $address['state']
						);
						
			if ($xml_property->response) {
				
				$estimate = array(
					'amount' => '$' . number_format(json_decode($xml_property->response->results->result->zestimate->amount), 2, ',', '.'),
					'last_updated' => current($xml_property->response->results->result->zestimate->{'last-updated'})
				);
				
			}
			
		}
		
		return response()->json($estimate, 200);
		
	}

}
