// 1. Shared Service (modal.service.ts)
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { BusinessListingCriteria, CommercialPropertyListingCriteria, UserListingCriteria } from '../../../../../bizmatch-server/src/models/main.model';

@Injectable({
  providedIn: 'root',
})
export class ModalService {
  private modalVisibleSubject = new BehaviorSubject<boolean>(false);
  private messageSubject = new BehaviorSubject<BusinessListingCriteria | CommercialPropertyListingCriteria | UserListingCriteria>(null);
  private resolvePromise!: (value: boolean) => void;

  modalVisible$: Observable<boolean> = this.modalVisibleSubject.asObservable();
  message$: Observable<BusinessListingCriteria | CommercialPropertyListingCriteria | UserListingCriteria> = this.messageSubject.asObservable();

  showModal(message: BusinessListingCriteria | CommercialPropertyListingCriteria | UserListingCriteria): Promise<boolean> {
    this.messageSubject.next(message);
    this.modalVisibleSubject.next(true);
    return new Promise<boolean>(resolve => {
      this.resolvePromise = resolve;
    });
  }

  accept(): void {
    this.modalVisibleSubject.next(false);
    this.resolvePromise(true);
  }

  reject(): void {
    this.modalVisibleSubject.next(false);
    this.resolvePromise(false);
  }
}
