import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, forkJoin, lastValueFrom, map, Observable, of, Subject } from 'rxjs';
import urlcat from 'urlcat';
import { User } from '../../../../bizmatch-server/src/models/db.model';
import { CombinedUser, FirebaseUserInfo, KeycloakUser, ResponseUsersArray, UserListingCriteria, UserRole, UsersResponse } from '../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root',
})
export class UserService {
  private apiBaseUrl = environment.apiBaseUrl;

  private userSource = new Subject<User>();
  currentUser = this.userSource.asObservable();

  constructor(private http: HttpClient) {}

  changeUser(user: User) {
    this.userSource.next(user);
  }
  // -----------------------------
  // DB services
  // -----------------------------
  async save(user: User): Promise<User> {
    return await lastValueFrom(this.http.post<User>(`${this.apiBaseUrl}/bizmatch/user`, user));
  }
  async saveGuaranteed(user: User): Promise<User> {
    return await lastValueFrom(this.http.post<User>(`${this.apiBaseUrl}/bizmatch/user/guaranteed`, user));
  }
  async getById(id: string): Promise<User> {
    return await lastValueFrom(this.http.get<User>(`${this.apiBaseUrl}/bizmatch/user/${id}`));
  }
  async getByMail(mail: string, hideLoading: boolean = true): Promise<User> {
    const url = urlcat(`${this.apiBaseUrl}/bizmatch/user`, { mail });
    let headers = new HttpHeaders();
    if (hideLoading) {
      headers = headers.set('X-Hide-Loading', 'true');
    }
    return await lastValueFrom(this.http.get<User>(url, { headers }));
  }
  async search(criteria?: UserListingCriteria): Promise<ResponseUsersArray> {
    return await lastValueFrom(this.http.post<ResponseUsersArray>(`${this.apiBaseUrl}/bizmatch/user/search`, criteria));
  }
  getNumberOfBroker(criteria?: UserListingCriteria): Observable<number> {
    return this.http.post<number>(`${this.apiBaseUrl}/bizmatch/user/findTotal`, criteria);
  }
  getKeycloakUser(id: string): Promise<KeycloakUser> {
    return lastValueFrom(this.http.get<KeycloakUser>(`${this.apiBaseUrl}/bizmatch/auth/users/${id}`));
  }
  async updateKeycloakUser(keycloakUser: KeycloakUser): Promise<void> {
    await lastValueFrom(this.http.put<void>(`${this.apiBaseUrl}/bizmatch/auth/users/${keycloakUser.id}`, keycloakUser));
  }
  // -------------------------------
  // ADMIN SERVICES
  // -------------------------------
  /**
   * Ruft alle Benutzer mit Paginierung ab
   */
  getAllUsers(maxResults?: number, pageToken?: string): Observable<UsersResponse> {
    let params = new HttpParams();

    if (maxResults) {
      params = params.set('maxResults', maxResults.toString());
    }

    if (pageToken) {
      params = params.set('pageToken', pageToken);
    }

    return this.http.get<UsersResponse>(`${this.apiBaseUrl}/bizmatch/auth`, { params });
  }

  /**
   * Ruft Benutzer mit einer bestimmten Rolle ab
   */
  getUsersByRole(role: UserRole): Observable<{ users: FirebaseUserInfo[] }> {
    return this.http.get<{ users: FirebaseUserInfo[] }>(`${this.apiBaseUrl}/bizmatch/auth/role/${role}`);
  }

  /**
   * Ändert die Rolle eines Benutzers
   */
  setUserRole(uid: string, role: UserRole): Observable<{ success: boolean }> {
    return this.http.post<{ success: boolean }>(`${this.apiBaseUrl}/bizmatch/auth/${uid}/role`, { role });
  }

  // -------------------------------
  // OLDADMIN SERVICES
  // -------------------------------

  getKeycloakUsers(): Observable<KeycloakUser[]> {
    return this.http.get<KeycloakUser[]>(`${this.apiBaseUrl}/bizmatch/auth/user/all`).pipe(
      catchError(error => {
        console.error('Fehler beim Laden der Keycloak-Benutzer', error);
        return of([]);
      }),
    );
  }

  getAppUsers(): Observable<User[]> {
    return this.http.get<User[]>(`${this.apiBaseUrl}/bizmatch/user/user/all`).pipe(
      catchError(error => {
        console.error('Fehler beim Laden der App-Benutzer', error);
        return of([]);
      }),
    );
  }


  /**
   * Lädt alle Benutzer aus den verschiedenen Quellen und kombiniert sie.
   * @returns Ein Observable mit einer Liste von CombinedUser.
   */
  loadUsers(): Observable<CombinedUser[]> {
    return forkJoin({
      keycloakUsers: this.getKeycloakUsers(),
      appUsers: this.getAppUsers(),

    }).pipe(
      map(({ keycloakUsers, appUsers }) => {
        const combinedUsers: CombinedUser[] = [];

        // Map App Users mit Keycloak und Stripe Subscription
        appUsers.forEach(appUser => {
          const keycloakUser = keycloakUsers.find(kcUser => kcUser.email.toLowerCase() === appUser.email.toLowerCase());

          // const stripeSubscription = appUser.subscriptionId ? stripeSubscriptions.find(sub => sub.id === appUser.subscriptionId) : null;
          const stripeUser = null;
          const stripeSubscription = null;
          combinedUsers.push({
            appUser,
            keycloakUser
          });
        });

        return combinedUsers;
      }),
      catchError(err => {
        console.error('Fehler beim Kombinieren der Benutzer', err);
        return of([]);
      }),
    );
  }
  async deleteCustomerFromStripe(customerId: string): Promise<void> {
    await lastValueFrom(this.http.delete(`${this.apiBaseUrl}/bizmatch/payment/customer/${customerId}`));
  }
}
