import { Injectable, inject, signal } from '@angular/core';
import { take } from 'rxjs/operators';
import { Connector } from '../../../../core/integrations/models/connector.model';
import { CONNECTOR_GATEWAY } from '../../../../core/integrations/services/connector-gateway.token';

@Injectable({ providedIn: 'root' })
export class AdminIntegrationsFacade {
  private readonly gateway = inject(CONNECTOR_GATEWAY);

  readonly connectors = signal<Connector[]>([]);
  readonly loading = signal(false);

  load(): void {
    this.loading.set(true);
    this.gateway.loadConnectors().pipe(take(1)).subscribe(items => {
      this.connectors.set(items);
      this.loading.set(false);
    });
  }

  pause(id: string): void {
    this.gateway.pause(id).pipe(take(1)).subscribe(() => this.load());
  }

  resume(id: string): void {
    this.gateway.resume(id).pipe(take(1)).subscribe(() => this.load());
  }
}
