import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, lastValueFrom } from 'rxjs';
import { BusinessListing } from '../../../../bizmatch-server/src/models/db.model';
import {
  BusinessListingCriteria,
  CommercialPropertyListingCriteria,
  ListingType,
  ResponseBusinessListingArray,
  ResponseCommercialPropertyListingArray,
  StatesResult,
} from '../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../environments/environment';

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

  async getListings(
    criteria: BusinessListingCriteria | CommercialPropertyListingCriteria,
    listingsCategory: 'business' | 'professionals_brokers' | 'commercialProperty',
  ): Promise<ResponseBusinessListingArray | ResponseCommercialPropertyListingArray> {
    const result = await lastValueFrom(this.http.post<ResponseBusinessListingArray | ResponseCommercialPropertyListingArray>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/find`, criteria));
    return result;
  }
  getNumberOfListings(criteria: BusinessListingCriteria | CommercialPropertyListingCriteria, listingsCategory: 'business' | 'commercialProperty'): Observable<number> {
    return this.http.post<number>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/findTotal`, criteria);
  }
  async getListingsByPrompt(criteria: BusinessListingCriteria | CommercialPropertyListingCriteria): Promise<BusinessListing[]> {
    const result = await lastValueFrom(this.http.post<BusinessListing[]>(`${this.apiBaseUrl}/bizmatch/listings/business/search`, criteria));
    return result;
  }
  getListingById(id: string, listingsCategory?: 'business' | 'commercialProperty'): Observable<ListingType> {
    const result = this.http.get<ListingType>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/${id}`);
    return result;
  }
  getListingsByEmail(email: string, listingsCategory: 'business' | 'commercialProperty'): Promise<ListingType[]> {
    return lastValueFrom(this.http.get<BusinessListing[]>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/user/${email}`));
  }
  async save(listing: any, listingsCategory: 'business' | 'professionals_brokers' | 'commercialProperty') {
    if (listing.id) {
      return await lastValueFrom(this.http.put<ListingType>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}`, listing));
    } else {
      return await lastValueFrom(this.http.post<ListingType>(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}`, listing));
    }
  }
  async getAllStates(listingsCategory?: 'business' | 'commercialProperty'): Promise<StatesResult[]> {
    const result = lastValueFrom(this.http.get<StatesResult[]>(`${this.apiBaseUrl}/bizmatch/listings/business/states/all`));
    return result;
  }
  async deleteBusinessListing(id: string) {
    await lastValueFrom(this.http.delete<ListingType>(`${this.apiBaseUrl}/bizmatch/listings/business/${id}`));
  }
  async deleteCommercialPropertyListing(id: string, imagePath: string) {
    await lastValueFrom(this.http.delete<ListingType>(`${this.apiBaseUrl}/bizmatch/listings/commercialProperty/${id}/${imagePath}`));
  }
}
