import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, map } from 'rxjs';
import { Bot, BotWithToken } from '../models/bot.models';

@Injectable({ providedIn: 'root' })
export class BotService {
  private readonly http = inject(HttpClient);
  private readonly base = '/api/account/bots';
  private readonly officialBase = '/api/account/official-bots';

  list(): Observable<Bot[]> {
    return this.http.get<Bot[]>(this.base);
  }

  listOfficial(): Observable<Bot[]> {
    return this.http.get<Bot[]>(this.officialBase);
  }

  create(name: string): Observable<BotWithToken> {
    return this.http.post<BotWithToken>(this.base, { name });
  }

  rotateToken(botId: string): Observable<string> {
    return this.http.post<{ token: string }>(`${this.base}/${botId}/rotate-token`, null)
      .pipe(map(r => r.token));
  }

  delete(botId: string): Observable<void> {
    return this.http.delete<void>(`${this.base}/${botId}`);
  }
}
