import { Component } from '@angular/core';
import { BusinessListing, CommercialPropertyListing, User } from '../../../../../../bizmatch-server/src/models/db.model';
import { KeycloakUser } from '../../../../../../bizmatch-server/src/models/main.model';
import { ConfirmationComponent } from '../../../components/confirmation/confirmation.component';
import { ConfirmationService } from '../../../components/confirmation/confirmation.service';
import { AuthService } from '../../../services/auth.service';
import { ListingsService } from '../../../services/listings.service';
import { SelectOptionsService } from '../../../services/select-options.service';
import { SharedModule } from '../../../shared/shared/shared.module';
import { map2User } from '../../../utils/utils';

@Component({
  selector: 'app-favorites',
  standalone: true,
  imports: [SharedModule, ConfirmationComponent],
  templateUrl: './favorites.component.html',
  styleUrl: './favorites.component.scss',
})
export class FavoritesComponent {
  user: KeycloakUser;
  // listings: Array<ListingType> = []; //= dataListings as unknown as Array<BusinessListing>;
  favorites: Array<BusinessListing | CommercialPropertyListing | User>;
  constructor(private listingsService: ListingsService, public selectOptions: SelectOptionsService, private confirmationService: ConfirmationService, private authService: AuthService) { }
  async ngOnInit() {
    const token = await this.authService.getToken();
    this.user = map2User(token);
    const result = await Promise.all([await this.listingsService.getFavoriteListings('business'), await this.listingsService.getFavoriteListings('commercialProperty'), await this.listingsService.getFavoriteListings('user')]);
    this.favorites = [...result[0], ...result[1], ...result[2]] as Array<BusinessListing | CommercialPropertyListing | User>;
  }
  async confirmDelete(listing: BusinessListing | CommercialPropertyListing | User) {
    const confirmed = await this.confirmationService.showConfirmation({ message: `Are you sure you want to remove this listing from your Favorites?` });
    if (confirmed) {
      // this.messageService.showMessage('Listing has been deleted');
      this.deleteListing(listing);
    }
  }
  async deleteListing(listing: BusinessListing | CommercialPropertyListing | User) {
    if ('listingsCategory' in listing) {
      if (listing.listingsCategory === 'business') {
        await this.listingsService.removeFavorite(listing.id, 'business');
      } else {
        await this.listingsService.removeFavorite(listing.id, 'commercialProperty');
      }
    } else {
      await this.listingsService.removeFavorite(listing.id, 'user');
    }
    const result = await Promise.all([await this.listingsService.getFavoriteListings('business'), await this.listingsService.getFavoriteListings('commercialProperty'), await this.listingsService.getFavoriteListings('user')]);
    this.favorites = [...result[0], ...result[1], ...result[2]] as Array<BusinessListing | CommercialPropertyListing | User>;
  }

  isBusinessOrCommercial(listing: any): boolean {
    return !!listing.listingsCategory;
  }
}
