import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CityAndStateResult, CountyResult, GeoResult } from '../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root',
})
export class GeoService {
  private apiBaseUrl = environment.apiBaseUrl;
  constructor(private http: HttpClient) {}

  findCitiesStartingWith(prefix: string, state?: string): Observable<GeoResult[]> {
    const stateString = state ? `/${state}` : '';
    return this.http.get<GeoResult[]>(`${this.apiBaseUrl}/bizmatch/geo/${prefix}${stateString}`);
  }
  findCitiesAndStatesStartingWith(prefix: string): Observable<CityAndStateResult[]> {
    return this.http.get<CityAndStateResult[]>(`${this.apiBaseUrl}/bizmatch/geo/citiesandstates/${prefix}`);
  }
  findCountiesStartingWith(prefix: string, states?: string[]): Observable<CountyResult[]> {
    return this.http.post<CountyResult[]>(`${this.apiBaseUrl}/bizmatch/geo/counties`, { prefix, states });
  }
}
