import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs';
import { Dashboard } from '../model/Dashboard';
import {
  GRAFANA_API_BASE_URL,
  GRAFANA_API_TOKEN
} from 'src/modules/common/type/injection-token.type';

@Injectable({
  providedIn: 'root'
})
export class GrafanaApiService {
  constructor(
    @Inject(GRAFANA_API_BASE_URL) private grafanaApiBaseUrl: string,
    @Inject(GRAFANA_API_TOKEN) private grafanaApiToken: string,
    private httpClient: HttpClient
  ) {}

  public getDashboards(): Observable<Dashboard[]> {
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: `Bearer ${this.grafanaApiToken}`
    });

    return this.httpClient.get<Dashboard[]>(
      `${this.grafanaApiBaseUrl}/search`,
      {
        headers: headers
      }
    );
  }
}
